Sunday, September 9, 2012

what is soap service in php example with php




The Soap Web services platform is XML and HTTP request. XML provides a language which can be used between different platforms and programming languages. The HTTP protocol is the most used Internet protocol. Web services platform elements are SOAP (Simple Object Access Protocol), UDDI (Universal Description, Discovery and Integration), WSDL (Web Services Description Language).
Here I have talking about SOAP, NuSOAP and how it will use in PHP with example. So now let's start. Simple Object Access Protocol (SOAP) is a simple XML-based protocol to let applications exchange information over HTTP.  Today's Web Service (SOAP) is most popular and it provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
Actually, the SOAP API itself has not been deprecated-- just some of the functions. NuSOAP is a PHP library that allows you to send and receive SOAP messages. NuSOAP is a group of PHP classes that allow developers to create and consume SOAP services. It does not require any special PHP extensions. To use NuSOAP You must download NuSOAP library. Click Here to download library first.
Once you have downloaded a library file, you simply need to place it in your code tree so that you can include it from your PHP code. For my examples, I placed it in the same directory as the sample code itself.
I will start with the ubiquitous "Hello, World" example. This will demonstrate the basic coding of NuSOAP clients and servers.
I am going to start with the server code. The server exposes a single SOAP method named "Hello", which takes a single string parameter for input and returns a string. Hopefully, the comments within the code provide sufficient explanation. For example server.php is a file name.
<?php

 // include NuSOAP class for service call
 require_once('nusoap.php');
 // Create the server instance
 $server = new soap_server;

 // Register the method to expose
 $server->register('hello');

  // Define the method as a PHP function
  function hello($name) 
  {
   return 'Hello, ' . $name; 
  }

  // Use the request to (try to) invoke the service
  $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? 
               $HTTP_RAW_POST_DATA : '';

  $server->service($HTTP_RAW_POST_DATA);

?>
Now create a client side Script which will save it to the file clients.php. There are a few important things to note. First, when the instance of soap client is created, the parameter specified is the URL to the service. Second, when calling the service, the first parameter is the service name. This must match with the method registered within server.php. Finally, the second parameter in the call is an array of parameters that will be passed to the SOAP service method. Since the hello method of server.php requires a single parameter, this array has one element.
<?php
 
 // Pull in the NuSOAP code
 require_once('nusoap.php');
 // Create the client instance
 $client = new nusoap_client('http://localhost/xml-webservice/server.php');
 // Check for an error
 $err = $client->getError();

 if ($err) 
 {
     // Display the error
     echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
     // At this point, you know the call that follows will fail
 }

 // Call the SOAP method
 $result = $client->call('hello', array('name' => 'Scott'));

 // Check for a fault 
 if ($client->fault) 
 {
      echo '<h2>Fault</h2><pre>';
      print_r($result);
      echo '</pre>';
 }
  else
  {
    
  // Check for errors
      $err = $client->getError();
      if ($err) 
  {
          // Display the error
          echo '<h2>Error</h2><pre>' . $err . '</pre>';
      }
   else 
   {
          // Display the result
          echo '<h2>Result</h2><pre>';
          print_r($result);
       echo '</pre>';
      }
 }
?>
NuSOAP also provides a debug information. Adding the following to the client code will display this  debugging information.
/// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
I showed above code how  to display the SOAP request and response. Here is what the request from the client..php looks like.
Result

Hello, Scott

Request

POST /xml-webservice/server.php HTTP/1.0 
 Host: localhost  
User-Agent: NuSOAP/0.7.3 (1.114)  
Content-Type: text/xml; charset=ISO-8859-1  
SOAPAction: ""  
Content-Length: 500    
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1564:hello xmlns:ns1564="http://tempuri.org">
<name xsi:type="xsd:string">Scott</name>
</ns1564:hello></SOAP-ENV:Body></SOAP-ENV:Envelope>

Response

HTTP/1.1 200 OK  
Date: Tue, 29 Sep 2009 08:47:02 GMT  
Server: Apache/2.2.3 (Win32) DAV/2 mod_ssl/2.2.3 OpenSSL/0.9.8d mod_autoindex_color PHP/5.2.0
X-Powered-By: PHP/5.2.0  
X-SOAP-Server: NuSOAP/0.7.3 (1.114)  
Content-Length: 518  
Connection: close  
Content-Type: text/xml; charset=ISO-8859-1    
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><ns1:helloResponse xmlns:ns1="http://tempuri.org">
<return xsi:type="xsd:string">Hello, Scott</return>
</ns1:helloResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
You can CLICK HERE full source code 

Saturday, September 8, 2012

simple create capcha code using php

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: bikash ranjan nayak
* Copyright: 2012 bikash

* Updated: 07/09/12
* Requirements: PHP 4/5 with GD and

class CaptchaSecurityImagesbybikash {

    var $font = &#039;monofont.ttf&#039;;

    function generateCode($characters) {
        /* list all possible characters, similar looking characters and vowels have been removed */
        $possible = &#039;23456789bcdfghjkmnpqrstvwxyz&#039;;
        $code = &#039;&#039;;
        $i = 0;
        while ($i generateCode($characters);
        /* font size will be 75% of the image height */
        $font_size = $height * 0.75;
        $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);
        $text_color = imagecolorallocate($image, 20, 40, 100);
        $noise_color = imagecolorallocate($image, 100, 120, 180);
        /* generate random dots in background */
        for( $i=0; $i&lt;($width*$height)/3; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
        }
        /* generate random lines in background */
        for( $i=0; $ifont, $code) or die('Error in imagettfbbox function');
        $x = ($width - $textbox[4])/2;
        $y = ($height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this-&gt;font , $code) or die('Error in imagettftext function');
        /* output captcha image to browser */
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
        $_SESSION['security_code'] = $code;
    }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) &amp;&amp; $_GET['characters'] &gt; 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImagesbybikash($width,$height,$characters);

?>

<img src="/wp-content/themes/avd/CaptchaSecurityImages.php?width=162&amp;height=45&amp;characters=5" />