Guest User

test2api

a guest
May 16th, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2.     //Data, connection, auth
  3.     $dataFromTheForm = $_POST['fieldName']; // request data from the form
  4.     $soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
  5.     $soapUser = "username";  //  username
  6.     $soapPassword = "password"; // password
  7.  
  8.     // xml post structure
  9.  
  10.     $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
  11.                        <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/">
  12.                          <soap:Body>
  13.                            <GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your\'s WSDL URL
  14.                              <PRICE>'.$dataFromTheForm.'</PRICE>
  15.                            </GetItemPrice >
  16.                          </soap:Body>
  17.                        </soap:Envelope>';   // data from the form, e.g. some ID number
  18.  
  19.     $headers = array(
  20.         "Content-type: text/xml;charset=\"utf-8\"",
  21.         "Accept: text/xml",
  22.         "Cache-Control: no-cache",
  23.         "Pragma: no-cache",
  24.         "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice",
  25.         "Content-length: ".strlen($xml_post_string),
  26.     ); //SOAPAction: your op URL
  27.  
  28.     $url = $soapUrl;
  29.  
  30.     // PHP cURL  for https connection with auth
  31.     $ch = curl_init();
  32.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  33.     curl_setopt($ch, CURLOPT_URL, $url);
  34.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  35.     curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
  36.     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  37.     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  38.     curl_setopt($ch, CURLOPT_POST, true);
  39.     curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
  40.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  41.  
  42.     // converting
  43.     $response = curl_exec($ch);
  44.     curl_close($ch);
  45.  
  46.     // converting
  47.     $response1 = str_replace("<soap:Body>", "", $response);
  48.     $response2 = str_replace("</soap:Body>", "", $response1);
  49.  
  50.     // convertingc to XML
  51.     $parser = simplexml_load_string($response2);
  52.     // user $parser to get your data out of XML response and to display it.
Add Comment
Please, Sign In to add comment