Send SOAP XML Request via PHP

Aim
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");
 3. 
 4. $url= $argv[1];
 5. 
 6. echo "url\n$url\n";
 7. 
 8. preg_match("/https?:\/\/([^\/]*)(.*)/", $url, $matches);
 9. $host=$matches[1];
10. $request=$matches[2];
11. 
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";
20. 
21. echo "header\n$header\n";
22. 
23. $header .=  $mxml;
24. 
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);
29. 
30. $rxml=curl_exec($yt);
31. echo "sent\n$mxml\n";
32. echo "received\n$rxml\n";
33. 
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 https://myparlay.com:445/payment_oneoff_ws_ctx_path/AmountCharging < xmltosend.xml

5 comments:

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 much.......it 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
Training

Dot Net Interview Questions