Monday, August 31, 2015

Install and Configure PHP/Apache on Amazon EC 2 with Ubuntu (With SFTP)

Hello,

Recently for one of my project we moved our web application to Amazon EC 2. So we had to configure Aapche and setup SFTP there. I faced some difficulties in setting this up so here in blog I am going to explain you the process so it will help you.

First of all login to SSH on your Amazon EC 2 instance. For that first you have to download public key from your amazon EC 2 instance.  You can use public IP of your EC 2 instance to login to SSH.

Open your terminal and type following command.

ssh -i "YourKey.pem" ubuntu@your_ip_address

once you are logged in first of we have to install apache. For that run following command.


sudo apt-get install apache2 mysql-server php5 php5-mysql libapache2-mod-php5

This will install apache, php and MySql with single command. During installation it will ask you for root password of MySql. Once installation is done type public ip in browser and it should show apache startup page. If you see this page that means apache installed. Now lets test PHP.

Go to your apache root directory with following command

cd /var/www/html

There will be one index.html file. We have to remove this and create new file for testing.

sudo rm index.html

sudo vim index.php

This will open vim editor, press i and now you can type in file. Let's create simple php file

<?php
     echo "Hello AWS";
?>


press esc and : and type wq this will save file. Now again type public IP in browser and it should show Hello AWS on browser.

Now let's create new SFTP user. But for that first we have to allow password authentication for SSH. Type following command.

cd /etc/ssh
sudo vim sshd_config

Find allowPasswordAuthetnciation. It will be set as no, make it yes and save the file and restart SSH service.

sudo restart ssh

Now we can add user with password.

sudo adduser --home /var/www/html username

It will ask for password. Now user is created with same group name. Now we have to make this user, owner of /var/www/html so we can edit files via FileZilla. Now lets fist change ownership of folders and files.

sudo chown -R root:username /var/www/html
sudo find /var/www/html -type f -exec chmod 664 {} \;
sudo find /var/www/html -type d -exec chmod 775 {} \;
sudo find /var/www/html -type d -exec chmod g+s {} \;

That's it now you can use this username and password with SFTP in FileZilla. Hope this helps you.


Thursday, August 27, 2015

Android Cordova 4.0.2 Webview Intercept Requests

Recently I was working on Android cordova app where I was using cordova 4.0.2 version. In this project I have to intercept urls loaded in webview and do some stuff.

In earlier cordova version we can do following.

myWebView.setWebViewClient(new WebViewClient() {
         public boolean shouldOverrideUrlLoading(WebView view, String url)  {
         }
});

But in newer version of cordova this will not work. So what to do? Here is this blog I am going to explain it.

First of all lets get webView


myWebView = (WebView) this.appView.getView();

Then add WebViewClient to webView.

myWebView.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine) this.appView.getEngine()){
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
          }
          public void onPageFinished(WebView view, String url) {
                   super.onPageFinished(view, url);
          }
});

As you can see in cordova newer version they have added new class SystemWebViewClient in cordova lib that we have to use for accessing functions like shouldOverrideUrlLoading and onPageFinished.

Hope this helps you. 

Monday, August 24, 2015

PHP Parse SOAP XML Response

Recently in one of my project we were using SOAP service which was having XML response that we have to parse with PHP. For some reason PHP SOAP client was giving some because it XML response was using name namespaces and that was not parsed correctly with SOAP client. Here in this blog I am going to explain how to call XML web service without using SOAP client and parse response with PHP.

We will use PHP cURL. First lets create XML post data.

$postString = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
<GetProducts xmlns="http://ws.sourcingcity.co.uk/">
  <query>pen</query>
  <username>username</username>
  <password>password</password>
  <pageindex>1</pageindex>
  <pagesize>20</pagesize>
</GetProducts>
  </soap:Body>
</soap:Envelope>';

So that's our post data. Now lets create cURL request.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ws.sourcingcity.co.uk/ws3dpromo.asmx?op=GetProducts');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: text/xml; charset=utf-8',
                                            'Connection: Keep-Alive',
                                            'SOAPAction: http://ws.sourcingcity.co.uk/GetProducts'
                                            ));    
$result = curl_exec($ch);
curl_close($ch);

Now you will get XML response in $result which is in string format. We will use SimpleXML class of PHP.

$soap = simplexml_load_string($result);

That will load SimpleXML object. Now we will register namespace.

$soap->registerXPathNamespace('ns1', 'http://ws.sourcingcity.co.uk/');

Now you can use XPATH to go to specific nodes.

$productsResult = $soap->xpath('//ns1:GetProductsResponse/ns1:GetProductsResult')[0]->Products;

for($i=0;$iProduct);$i++){
$product = $productsResult->Product[$i];
echo "Product ".$i;
echo "Name : ".$product->Name;
echo "SCRef : ".$product->SCRef;
echo "Description : ".$product->Description;
echo "PriceRange : ".$product->PriceRange;
echo "ProductType : ".$product->ProductType;
echo "ProductGroups : ".$product->ProductGroups;
echo "Colours : ".$product->Colours;
echo "ThumbImageURL : ".$product->ThumbImageURL;
echo "LargeImageURL : ".$product->LargeImageURL;
}

That's it. In the same way you can parse any other XML response.