Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & XML - How to generate a soap request in PHP from this XML?

Tags:

php

soap

xml

I am completly new to SOAP operations.

I have been provided with an XML document (SOAP) to get some collection points for a shipping method.

From the manual located here:

http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint

I can see that I need to use the following SOAP request:

POST /package/package_1.3/packageservices.asmx HTTP/1.1
Host: privpakservices.schenker.nu
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://privpakservices.schenker.nu/SearchCollectionPoint"

<?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>
    <SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/">
      <customerID>long</customerID>
      <key>string</key>
      <serviceID>string</serviceID>
      <paramID>int</paramID>
      <address>string</address>
      <postcode>string</postcode>
      <city>string</city>
      <maxhits>int</maxhits>
    </SearchCollectionPoint>
  </soap:Body>
</soap:Envelope>

The thing is that i don't know how to send this as a request using PHP, and how to get the response.

Any help to pinpoint me in the right direction, is much appreciated.

UPDATE

I can read the response data with var_dump. However, I am not able to read individual element data.

I need to read data as below

foreach($parser as $row) {
  echo $row->customerID;
  echo $row->key;
  echo $row->serviceID;  
}
like image 357
Hans Preutz Avatar asked Sep 02 '13 11:09

Hans Preutz


People also ask

What is PHP?

What is PHP? PHP is a script on the server-side used for the creation of Static or Dynamic Web sites or Web applications. PHP is a pre-processor for hypertext, which used to stand for home pages. The software used to build web applications is an open-source, server-side scripting language.

What is the use of PHP operators?

Operators are used to perform operations on variables and values. The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. The PHP assignment operators are used with numeric values to write a value to a variable.

What is the difference between&&and and and in PHP?

In addition to using the and keyword, PHP uses && as the logical AND operator: The && and and operators return the same result. The only difference between the && and and operators are their precedences.

Is there a free interactive PHP tutorial?

Welcome to the learn-php.org free interactive PHP tutorial. Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the PHP programming language. There is no need to download anything - just click on the chapter you wish to begin from, and follow the instructions.


2 Answers

If anyone should be interested, i have provided the correct answer:

$soapUrl = "http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint";

$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/"><customerID>XXX</customerID><key>XXXXXX-XXXXXX</key><serviceID></serviceID><paramID>0</paramID><address>RiksvŠgen 5</address><postcode>59018</postcode><city>Mantorp</city><maxhits>10</maxhits></SearchCollectionPoint></soap12:Body></soap12:Envelope>';

$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: privpakservices.schenker.nu",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
); 

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch); 
curl_close($ch);

$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);

$parser = simplexml_load_string($response2);
like image 113
Hans Preutz Avatar answered Oct 21 '22 14:10

Hans Preutz


following example might help you.

SOAP XML schema

<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
        <x:Header/>
        <x:Body>
            <web:NumberToDollars>
                <web:dNum>10</web:dNum>
            </web:NumberToDollars>
        </x:Body>
    </x:Envelope>

PHP code

  $wsdl = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';


    try{
        $clinet=new SoapClient($wsdl);

        $ver =array("dNum"=>"2002");
        $quates=$clinet->NumberToDollars($ver);

        var_dump($quates);


    }

    catch(SoapFault $e)
    {
        echo $e->getMessage();
    }
like image 28
danish dani Avatar answered Oct 21 '22 13:10

danish dani