Tuesday, 13 January 2015

How to create webservice in php with SOAP Technology

How to create webservice in php with SOAP Technology

Here we will learn how to create a SOAP based web service and know how to use a web service created with SOAP.

For creating webservice you must know the basics of following.
What is Web Services
Web Services is Interaction between two machines over a network. It use SOAP, REST, and XML-RPC as a means of communication. A Web service always needs a network for its operation.
Know Difference between WebService and API


What is SOAP
SOAP stands for Simple Object Access Protocol. SOAP is an XML-based protocol for exchanging information between two computers over the internet. It enables you to Remote Procedure Calls (RPC) transported via HTTP.
More About SOAP


Now Follow the following Simple Steps:
1. Donwnload the library File from http://sourceforge.net/projects/nusoap/.
2. Unzip/extract the downloaded zip file.
3. Copy the "lib" folder from unziped folder and past in root folder of your project.
   Suppose your project is myproject:
   Project Location: D:\wamp\www\myproject
   Library Location: D:\wamp\www\myproject\lib
4.Create two files soapServer.php and soapClient.php into myproject folder.
5. Add Following code in soapServer.php
/* Include library file **/ 
require_once ('lib/nusoap.php'); 

/** create the object of SoapServer **/
$serverObj = new soap_server;
$namespace = 'http://localhost/myproject/soapServer.php?wsdl';
$serverObj->configureWSDL('get_messageString', $namespace); 

//register a function that works on server 
$serverObj->register('get_message'); 

// create the function 
function get_message($your_name) 
{ 
return "Hello {$your_name}!"; 
} 

if ( !isset( $HTTP_RAW_POST_DATA ) ){
  $HTTP_RAW_POST_DATA =file_get_contents('php://input' );
} 
// create HTTP listener 
$serverObj->service($HTTP_RAW_POST_DATA);


6. Add Following code in soapClient.php
require_once ('lib/nusoap.php'); 
//Give it value at parameter 
$param = array( 'your_name' => 'Web Technology Experts Notes');

try{
      //Create object that referer a web services 
    $clientObj = new soapclient('http://localhost/myproject/soapServer.php'); 
    //Call a function at server and send parameters too 
    $response = $clientObj->call('get_message',$param); 
    //Process result 
    if($clientObj->fault){ 
      echo "FAULT: Code: ($clientObj->faultcode)"; 
      echo "String: ".$clientObj->faultstring; 
    }else { 
      echo $response; 
      } 
}catch(Exception $e){
    echo $e->getMessage();
}   

7. soapClient.php will use as client whereas soapServer.php will be used as server.

Monday, 12 January 2015

PHP Technical Interview Questions and Answers for Fresher and Experienced

PHP Technical Interview Questions and Answers for Fresher and Experienced



Question: How to convert string to array in php?
$string="cakephp and zend";
$array =explode('and',$string);
print_r($array);


Question: How to convert array to string in php?
$array = array('cakephp','zend');
$string =implode(' and ',$array);
echo $string;


Question: How to connect mysqli with php using Object-Oriented Way?
$host = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password);
//connect to server
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";


Question: How to connect mysqli with php using Procedural Way?
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host, $username, $password);
//connect to server
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";


Question: How to connect mysqli with php using PDO?
$host = "localhost";
$username = "root";
$password = "";
 $conn = new PDO("mysql:host=$host;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";


Question: Give Curl example using post method?
$postData = array(
   "site" => "web technology experts notes",
   "dailyuser" => "1000",
   "location" => "India"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
echo $output;




Question: How to destroy one session?
unset($_SESSION['object']);


Question: How to Destroys all data registered to a session?
session_destroy();


Question: How to delete a php file from server?
$file="full_path/filename.php"
unlink($file); //make sure you have enough permission to do delete the file.


Question: How to convert string to uppercase in php?
$string="cakephp and zend";
echo strtoupper($string);


Question: How to convert string to lowercase in php?
$string="CAKEPHP AND ZEND";
echo strtolower($string);


Question: How to convert first letter of string to uppercase in php?
$string="cakephp and zend";
echo ucwords($string);


Question: Difference between array_merge and array_combine in php?
array_merge example
$array1 = array('one','two');
$array2 = array(1,2);
$result = array_merge($array1,$array2);
print_r($result);

array_combine example
$array1 = array('one','two');
$array2 = array(1,2);
$result = array_combine($array1,$array2);
print_r($result);


Question: How to convert array to json in php?
$array = array('one','two');
echo json_encode($array); //use json_decode for decode 


Question: How to serialize an array in php?
$array = array('one','two');
echo serialize($array);//use unserialize for convert serialized string to array


Question: How to get ip address in php?
$_SERVER['REMOTE_ADDR']


Question: How to count the number of elements in an array?
$array = array('cakephp','zend');
sizeof($array);
count($array);


Question: How to call constructor of parent class?
parent::constructor()


Question: What is use of var_dump?
It is used to display the data-type and values of variable. For Example.
$name='WTEN';
var_dump($name);



Question: What is final class
It is class which can not be inherited.
Following are example of final class
final class baseclass{
        public function testmethod()  {
            echo  "base class method";
        }
}


Question: How can we define constants in PHP?
define('WTEN','Web Technology Experts Notes'); //How to define constants
echo WTEN;//How to use constants




Question: How to start displaying errors in PHP application ? Add following code in PHP.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

OR
Add following code in .htacess
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag  log_errors on