Creating a SOAP Request Using PHP/CURL with Password

Creating a SOAP request is great for debugging/testing a product. It seems to be difficult though to find a clean and simple code example on Google for some reason. You end up needing to look at several sites and piece it together. To many this process easier, here is a very basic SOAP request using PHP with cURL.

Be sure to set your server URL and username/password, if you’re using one. If your server doesn’t require authentication, you can comment out the CURLOPT_HTTPAUTH line.

$data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sungardhe:enterprise:ar:messages">
   <soapenv:Header/>
   <soapenv:Body>
      ..........
   </soapenv:Body>
</soapenv:Envelope>';


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://your.server.com:port/pci/TEST/v1_0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-Length: " . strlen($data)));
$output = curl_exec($ch);
curl_close($ch);

echo $output . "\n";

 

Leave a Reply