We can use the PHP cURL library to generate simple HTTP POST request. The following example shows you how to create a simple SOAP request using cURL.
Create the soap-server.php which write the SOAP request into soap-request.xml in web folder.
<?php $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $f = fopen("./soap-request.xml", "w"); fwrite($f, $HTTP_RAW_POST_DATA); fclose($f); ?>
The next step is creating the soap-client.php which generate the SOAP request using the cURL library and send it to the soap-server.php URL.
<?php $soap_request = "<?xml version=\"1.0\"?>\n"; $soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n"; $soap_request .= " <soap:Body xmlns:m=\"http://www.example.org/stock\">\n"; $soap_request .= " <m:GetStockPrice>\n"; $soap_request .= " <m:StockName>IBM</m:StockName>\n"; $soap_request .= " </m:GetStockPrice>\n"; $soap_request .= " </soap:Body>\n"; $soap_request .= "</soap:Envelope>"; $header = array( "Content-type: text/xml;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"run\"", "Content-length: ".strlen($soap_request), ); $soap_do = curl_init(); curl_setopt($soap_do, CURLOPT_URL, "http://localhost/php-soap-curl/soap-server.php" ); curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($soap_do, CURLOPT_TIMEOUT, 10); curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true ); curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($soap_do, CURLOPT_POST, true ); curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request); curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header); if(curl_exec($soap_do) === false) { $err = 'Curl error: ' . curl_error($soap_do); curl_close($soap_do); print $err; } else { curl_close($soap_do); print 'Operation completed without any errors'; } ?>
Enter the soap-client.php URL in browser to send the SOAP message. If success, Operation completed without any errors will be shown and the soap-request.xml will be created.
<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope>
Done =)
Reference:
Thank you very much. I just had some trouble with native SoapClient objects and I decided to send a raw xml message. Your post has helped me a lot.
LikeLike
Good to know that i could help. =)
LikeLike
Thanks very much for this post. It has been very informative!
I tried this code to send a raw SOAP request using authentication but all I get back from the server is the full wsdl file for the service. Does this method handle authentication as well over https?
Thanks again,
JB
LikeLike
If you want to send the HTTPS request, you have to make some change on the curl setting such as CURLOPT_SSL_VERIFYPEER.
I think the following post should help.
unitstep.net – Using cURL in PHP to access HTTPS (SSL/TLS) protected sites
LikeLike
Been trying to send through soapClient for over 2 weeks now. After deceiding to send it straight as an xml, I found your blog. Fixed the problem within 10 minutes.
Extreme simple solution, which gets the job done.
Maximus Kudos!
LikeLike
Thanks for your comment and i am glad to know that i could help. =D
LikeLike
thanks a lot really helpful
LikeLike
Thanks for your comment and you are welcome~ =D
LikeLike
Where does the soap-request.xml get created? I don’t see it and I’m not getting any errors..
mG
LikeLike
should be the same folder as soap-server.php
have u checked the folder permission?
LikeLike
I didn’t know SOAP but a job dropped on my lap that needed integration of a SOAP service without WDSL, which most other tutorials use, I was scratching my head as the service’s manual only provided the XML, no URI or WDSL link. Thanks for sharing your expertise, I totally know how to continue now, thanks!
LikeLike
Glad to know that this post could help and thanks for your comment~ =D
LikeLike
Hello I tried this and it all worked till the output data because I cant find it.
After testing the example above I tried adapting it to my SOAP site 🙂 but cant get it to work.
If possible would anyone please help me out?
I want to do a zipcode check for a database (postcodecheck).
https://pqcc.soap.dslorder.nl/pqcc/v7.0/pqcc.asmx
for the test you could use PostalCode=”1213xd” HouseNumber=”42″ HouseNumberAddition=”a”
I really need you’re help
LikeLike
You have to follow the request format including the http header and the soap request. Take a look on the testing request in this url.
LikeLike
Hi Thanks for your reply. I haven’t had time to test this because of my work. But am I understanding this correctly that I have to add the entire thing so the above xml and the below xml into my PHP script? Sorry for not understanding, i’m new to all of this and trying to find out .
LikeLike
yes, you have to construct the same http header and the same soap request.
LikeLike
Hello ykyuen,
First of all thanks for your reply long ago 🙂 .
Because of other projects I had to put all of this on hold.
At the moment I havent tried my own SOAP site yet, because I’m trying to get your instructions to work.
Everything works from creating the soap-server.php and soap-client.php and when I run the client I get a “Operation completed without any errors” sign. However, when I look at the xml output it is blanc. Do you also have this problem?
LikeLike
I;ve found the problem I had to change the first line of the Curl’s URL. Now I’ll have to adapt this to my SOAP witch is quite hard. Do you mind if I ask you for help if I have problems?
LikeLike
Thanks. I solved the issue. $HTTP_RAW_POST_DATA feature was DEPRECATED in PHP 5.6.0, and REMOVED as of PHP 7.0.0.
LikeLike
This method has the potential of being very useful since my wsdl/curl request has a long xml to do the query. All other soap methods seem to be too simple for a long xml soap request. I’m not seeing where to get the result of the query back – not success or failure. Any thoughts? Where is the query result after doing the above? Thanks!
LikeLike