How to Send Soap Xml via Curl and PHP

SOAP request in PHP with CURL

Tested and working!

  • with https, user & password

     <?php 
    //Data, connection, auth
    $dataFromTheForm = $_POST['fieldName']; // request data from the form
    $soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
    $soapUser = "username"; // username
    $soapPassword = "password"; // password

    // xml post structure

    $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
    <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/">
    <soap:Body>
    <GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your WSDL URL
    <PRICE>'.$dataFromTheForm.'</PRICE>
    </GetItemPrice >
    </soap:Body>
    </soap:Envelope>'; // data from the form, e.g. some ID number

    $headers = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice",
    "Content-length: ".strlen($xml_post_string),
    ); //SOAPAction: your op URL

    $url = $soapUrl;

    // PHP cURL for https connection with auth
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // converting
    $response = curl_exec($ch);
    curl_close($ch);

    // converting
    $response1 = str_replace("<soap:Body>","",$response);
    $response2 = str_replace("</soap:Body>","",$response1);

    // convertingc to XML
    $parser = simplexml_load_string($response2);
    // user $parser to get your data out of XML response and to display it.
    ?>

How can I send SOAP XML via Curl and PHP?

Thanx a lot buddy, your code has worked for me.

Here is the code:

$soap_do = curl_init(); 
curl_setopt($soap_do, CURLOPT_URL, $url );
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, $post_string);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($post_string) ));
curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);

$result = curl_exec($soap_do);
$err = curl_error($soap_do);

In PHP how to send signed xml as soap request using curl

Code
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
replaced with
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('soapRequest.xml'))

And it started working fine.

Instead of specifying value of file in $message variable and then setting $message in CURLOPT_POSTFIELDS, now using file_get_contents() to read file and set CURLOPT_POSTFIELDS.

Taken hint from PHP cURL: how to set body to binary data?

How to send XML soap request using php curl

Please check below code. It's working great.

class RevodocLeadSoapClient extends SoapClient {

function __construct($wsdl, $options) {
parent::__construct($wsdl, $options);
$this->server = new SoapServer($wsdl, $options);
}
public function __doRequest($request, $location, $action, $version) {
$result = parent::__doRequest($request, $location, $action, $version);
return $result;
}
function __anotherRequest($call, $params) {
$location = 'https://www.t1.revodoc.com/ws/services/RevoLeadWebService';
$action = 'http://localhost/'.$call;
$request = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:typens="https://secure.maventa.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<SOAP-ENV:Body>
<RevodocLead xmlns="http://localhost/">';
$request .= $params;
$request .= '</RevodocLead>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$result =$this->__doRequest($request, $location, $action, '1');
return $result;
}
}

// Create new SOAP client
$wsdl = 'https://www.t1.revodoc.com/ws/services/RevoLeadWebService?wsdl';
$client = new RevodocLeadSoapClient($wsdl, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'cache_ttl' => 86400,
'trace' => true,
'exceptions' => true,
));

$operations = "RevodocLead";

$soap_request .= "<inode>";
$soap_request .= "<version>1.0</version>";
$soap_request .= "<sourceuserid>test@test.in</sourceuserid>";
$soap_request .= "<sourceid>18641254455</sourceid>";
$soap_request .= "<dealercode>1458445284</dealercode>";
$soap_request .= "<created>18-12-2015 13:45EST</created>";
$soap_request .= "<browser>Android Browser</browser>";
$soap_request .= "<os>Macintosh</os>";
$soap_request .= "<agent>atinek@gmail.com</agent>";
$soap_request .= "<product>mortgage-renewal</product>";
$soap_request .= "<name>success</name>";
$soap_request .= "<address>success</address>";
$soap_request .= "<email>success</email>";
$soap_request .= "<phone>success</phone>";
$soap_request .= "<propertyvalue>325000</propertyvalue>";
$soap_request .= "<mortgage>300000</mortgage>";
$soap_request .= "<referralcode>F3S2A2</referralcode>";
$soap_request .= "<note>value</note>";
$soap_request .= "<futurecontact>yes</futurecontact>";
$soap_request .= "</inode>";

// Make the request
try {
$request = $client->__anotherRequest($operations, $soap_request);
} catch (SoapFault $e ){
echo "Last request:<pre>" . htmlentities($client->__getLastRequest()) . "</pre>";
exit();
}
//Print Response. #RevodocLeadResponse
echo '<pre><b>', htmlentities($request), '</b></pre>';

Soap requests with Curl PHP

After struggling for a week I was able to find something in this tutorial here on youtube https://www.youtube.com/watch?v=6V_myufS89A and I was able to send requests to the API successifuly, first read my xml format above before continuing with my solution

     $options = array('trace'=> true, "exception" => 0);

$client = new \SoapClient('your url to wsdl',$options);

//if you have Authorization parameters in your xml like mine above use SoapVar and SoapHeader as me below

$params = new \stdClass();
$params->alisonOrgId = 'value here';
$params->alisonOrgKey = 'value here';

$authentication_parameters = new \SoapVar($params,SOAP_ENC_OBJECT);

$header = new \SoapHeader('your url to wsdl','credentials',$authentication_parameters);

$client->__setSoapHeaders(array($header));

print_r($client->__soapCall("Function here",'Function parameter here or left it as null if has no parameter'));

//or call your function by

print_r($client->yourFunction($function_parameters));

}

Hope this will help someone out there struggling with soap requests that contains authentication informations

CURL request to soap service with standard xml envelope and pdf attachment

The problem can be considered solved.

The request failed due to a problem with the server receiving the call.

So, the PHP script that I reported can be a good way to make a call to a webservice soap that contains an xml envelope with some parameters and a binary MIME attachment.



Related Topics



Leave a reply



Submit