Showing posts with label soap. Show all posts
Showing posts with label soap. Show all posts

Thursday, December 19, 2013

SOAP request in PHP with CURL

Php code to soap action via curl:


<?php
$xml = '<?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>'.
            '<GetShirtInfo xmlns="http://api.soap.website.com/WSDL_SERVICE/">'.
                '<ItemId>15</ItemId>'.
            '</GetShirtInfo>'.
        '</soap:Body>'.
    '</soap:Envelope>';

$url = "https://api.soap.website.com/soap.asmx?wsdl";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
array_push($headers, "SOAPAction: http://api.soap.website.com/WSDL_SERVICE/GetShirtInfo");
if($xml != null) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
    array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "user_name:password"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>

And output would be like this:


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ShirtInfoResponse
            xmlns="http://api.soap.website.com/WSDL_SERVICE/">
            <ShirtInfo>
                <ItemId>15</ItemId>
                <ItemName>Some Item Name</ItemName>
                <ItemPrice>658.7</ItemPrice>
                <ItemCurrency>AUD</ItemCurrency>
            </ShirtInfo>
        </ShirtInfoResponse>
    </soap:Body>
</soap:Envelope>

Saturday, April 27, 2013

Php making a soap based server with basic authentication

Compared to the standard authentication in the rest of your applications, SOAP authentication isn't too complicated. There are two easy ways of integrating it into your SOAP server: using HTTP Basic authentication or processing a custom SOAP header.

Soap-server:


<?php
function pc_authenticate_user($username, $password)
{
    $is_valid = false;
    if(strlen(trim($username)) > 0 && trim($username) == "adm" && strlen(trim($password)) > 0 && trim($password) == "pwd") {
        $is_valid = true;
    }
    if ($is_valid) {
        return true;
    }
    else {
        return false;
    }
}
class pc_SOAP_return_time
{
    public function __construct() {
        // Throw SOAP fault for invalid username and password combo
        if (!pc_authenticate_user($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) ) {
            throw new SOAPFault("Incorrect username and password combination.", 401);
        }
    }

    function return_time($name, $lastName) {
        return "Time: ".time().", First Name: ".$name.", Last Name: ".$lastName;
    }

    function accept_xml() {
        $postData = file_get_contents("php://input");
        return $postData;
    }
}
$server = new SOAPServer(null, array('uri' => 'urn:pc_SOAP_return_time'));
$server->setClass('pc_SOAP_return_time');
$server->handle();

Soap-client


<?php
$opts = array(
    'location' => 'http://localhost/ci/dragon71/soap-check/server.php',
    'uri' => 'urn:pc_SOAP_return_time',
    'login' => 'adm',
    'password' => 'pwd'
);

$client = new SOAPClient(null, $opts);

$result = $client->__soapCall('return_time', array("Pritom", "Kumar"));
print_r($result);

Or

<?php
$xml = '<?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>'.
    '<accept_xml xmlns="http://localhost/ci/dragon71/soap-check/server.php/">'.
    '<ItemId>15</ItemId>'.
    '</accept_xml>'.
    '</soap:Body>'.
    '</soap:Envelope>';

$url = "http://localhost/ci/dragon71/soap-check/server.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
if($xml != null) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
    array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "adm:pwd"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
header('Content-Type: text/xml');
print_r($response);

Make sure you have soap enabled in your server machine. Write "phpinfo();" and look for the word "Soap Client" as below:



If you don't find above soap enabled then you have to enable it. Navigate explorer to /c/xampp/php and edit "php.ini" and uncomment the line marked in below image and restart server and check again if your server has soap enabled:




Php making a soap based wsdl server

First create a wsdl file under www/soap/ directory named suppose soap1.wsdl:

<?xml version="1.0"?>
<definitions name="WsdlSoap" targetNamespace="urn:WsdlSoap"
             xmlns:tns="urn:WsdlSoap" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             xmlns="http://schemas.xmlsoap.org/wsdl/">

    <message name="getMyName">
        <part name="name" type="string"/>
    </message>
    <message name="getMyNameResponse">
        <part name="return" type="string"/>
    </message>
    <portType name="WsdlSoapPort">
        <operation name="getMyName">
            <input message="tns:getMyName"/>
            <output message="tns:getMyNameResponse"/>
        </operation>
    </portType>
    <binding name="WsdlSoapBinding" type="tns:WsdlSoapPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="getMyName">
            <soap:operation soapAction="urn:WsdlSoapAction"/>
            <input>
                <soap:body use="encoded" namespace="urn:WsdlSoap" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </input>
            <output>
                <soap:body use="encoded" namespace="urn:WsdlSoap" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </output>
        </operation>
    </binding>
    <service name="WsdlSoapService">
        <port name="WsdlSoapPort" binding="tns:WsdlSoapBinding">
            <soap:address location="http://localhost/soap/server1.php"/>
        </port>
    </service>
</definitions>

/soap/server1.php

<?php
function getMyName($name)
{

    return "Your name: ".$name;

}

$server = new SoapServer("soap1.wsdl");

$server->addFunction("getMyName");

$server->handle();
?>

Call soap:
$soap = new SoapClient("http://localhost/soap/soap1.wsdl");
echo $soap-> getMyName ("Pritom Kumar");
And it prints: "Your name: Pritom Kumar".