Send SOAP XML Request via PHP

The aim of this example is to create a simple PHP script that cam be fed a url from the command line and xml from the standard in. It will then send the xml to the url and get the response. This works with http and secure http or https too. Use this PHP script to quickly and easily test your web services or third party web services. This also demonstrates reading from standard in or STDIN.

Save this file as soaper.php
 1. <?php
 2. ini_set('display_errors', "1");
 4. $url= $argv[1];
 6. echo "url\n$url\n";
 8. preg_match("/https?:\/\/([^\/]*)(.*)/", $url, $matches);
 9. $host=$matches[1];
10. $request=$matches[2];
12. $mxml=fread(STDIN,65536);
13. $yt =curl_init();
14. $header =   "POST $request  HTTP/1.0\r\n";
15. $header .=  "Host: $host\r\n";
16. $header .=  "Content-Type: text/xml\r\n";
17. $header .=  "Content-Length: ".strlen($mxml)."\r\n";
18. $header .=  "Content-Transfer-Encoding: text\r\n";
19. $header .=  "Connection-Close: close\r\n\r\n";
21. echo "header\n$header\n";
23. $header .=  $mxml;
25. curl_setopt($yt, CURLOPT_SSL_VERIFYPEER,0);
26. curl_setopt($yt, CURLOPT_URL, $url);
27. curl_setopt($yt, CURLOPT_CUSTOMREQUEST, $header);
28. curl_setopt($yt, CURLOPT_RETURNTRANSFER, true);
30. $rxml=curl_exec($yt);
31. echo "sent\n$mxml\n";
32. echo "received\n$rxml\n";
34. echo curl_error($yt);
35. ?>
Hide line numbers

You can run this example like below. This will feed the xml from the file text.xml and send it to the url https://myurl/myService.

php soaper.php https://myurl/myService < text.xml

Here is a real example of sending a charging request to the AmountCharging Parlay Interface.

php soaper.php < xmltosend.xml


Anonymous said...

Thanks...Really helpfull

Narasimha said...

Thank you so much.........

Please help me
When I run in my command prompt with a xml file as request, it displays output file as Version Mismatch in the fault string tag.

Thank in advance

righteous said...

Is your 'fault string tag' in the xml output? If so then the xml you are sending is probably not what the web service is expecting.

Anonymous said...

Thank you very is very useful for me.

Naviya Nair said...

Very interesting and good Explanation
ASP NET Training
ASP NET Training
ASP NET Online Training
C-Sharp Training
Dot Net Training in Chennai
Online .Net Training

MVC Training
WCF Training
Web-API Training
LINQ Training
Entity Framework

Dot Net Interview Questions