How to integrate SOAP API using curl in PHP?

Is there a way to make SOAP ( Simple Object Access Protocol ) API request using curl in PHP? Please explain.

Yes. We can integrate a SOAP API using curl in php. I can make you understand by giving an example so that you can understand well. SOAP is its own protocol which uses XML to make requests and it is more secure that REST API. I will explain about SOAP in different section, let us see how we can make SOAP request using curl. Please check the below example to integrate any of the SOAP API and will work fine:


$soapUrl = "https://ws.campaigner.com/2013/01/campaignmanagement.asmx?op=ListCampaigns"; // asmx URL of WSDL
        $soapUser = "XYZ";  //  username
        $soapPassword = "******"; // password

        // xml post structure

</p> <p>        $xml_post_string = &#39;<?xml version="1.0" encoding="utf-8"?><br />                             <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/"><br />                                   <soap:Body><br />                                     <ListCampaigns xmlns="https://ws.campaigner.com/2013/01"><br />                                       <authentication><br />                                         <Username>&#39;.$soapUser.&#39;</Username><br />                                         <Password>&#39;.$soapPassword.&#39;</Password><br />                                       </authentication><br />                                       <dateTimeFilter><br />                                         <FromDate>2019-03-01</FromDate><br />                                         <ToDate>2019-04-26</ToDate><br />                                       </dateTimeFilter><br />                                     </ListCampaigns><br />                                   </soap:Body><br />                                 </soap:Envelope>&#39;;   // data from the form, e.g. some ID number</p> <p>           $headers = array(<br />                         "Content-type: text/xml;charset=\"utf-8\"",<br />                         "Accept: text/xml",<br />                         "Cache-Control: no-cache",<br />                         "Pragma: no-cache",<br />                         "SOAPAction: https://ws.campaigner.com/2013/01/ListCampaigns", <br />                         "Content-length: ".strlen($xml_post_string),<br />                     ); //SOAPAction: your op URL</p> <p>            $url = $soapUrl;</p> <p>            // PHP cURL  for https connection with auth<br />             $ch = curl_init();<br />             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);<br />             curl_setopt($ch, CURLOPT_URL, $url);<br />             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br />             curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc<br />             curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);<br />             curl_setopt($ch, CURLOPT_TIMEOUT, 10);<br />             curl_setopt($ch, CURLOPT_POST, true);<br />             curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request<br />             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);</p> <p>            // converting<br />             $response = curl_exec($ch); <br />             curl_close($ch);</p> <p>            //echo $response;</p> <p>            // converting<br />             $response1 = str_replace("<soap:Body>","",$response);<br />             $response2 = str_replace("</soap:Body>","",$response1);</p> <p>            // convertingc to XML<br />             $parser = simplexml_load_string($response2);<br />             // user $parser to get your data out of XML response and to display it.</p> <p>

In above code you just need to place your own API username and password. If you face any problem any where you can comment here and you will get proper response of your queries.