How to Post Soap Request from PHP

How to create a PHP SOAP Request and Response

Something like this should help you get started. I am not 100% familiar with the NADA API so I don't know what valid values are for some of the parameters... you'll have to fill in the correct values (e.g. for Token, Period, VehicleType, and Region).

$clientV = new SoapClient('http://webservice.nada.com/vehicles/vehicle.asmx?wsdl',array('trace' => 1,'exceptions' => 1, 'cache_wsdl' => 0));

$params = new stdClass();
$params->Token = '';
$params->Period = 1;
$params->VehicleType = '';
$params->Vin = '5YFBURHE3FP331896';
$params->Region = 1;
$params->Mileage = 100;

$result = $clientV->getDefaultVehicleAndValueByVin(array('vehicleRequest' => $params));

How to build a correct SOAP request with PHP

"Is there a more direct approach where I could write the XML?"

By using a SoapVar and setting the encode parameter of the constructor to XSD_ANYXML you can write the raw XML.

There should be a way where the WSDL helps build the XML though.

You could try something like this:

$wsdl   = "http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl"; 
$client = new SoapClient($wsdl, array( 'soap_version' => SOAP_1_1,
'trace' => true,
));
try {

$xml = '<arg0>
<content>
<entry>
<key>1</key>
<value>
<![CDATA[
<table width="600">
<tr>
<td>
<font size="2" face="Arial">Our powerful algorithms already
found a matching profile that matches your criteria:
<br>Celina72 </font>
<img src="http://mypath/to/my/image.gif" width="50"
height="50" border="0" />
</td>]]></value>
</entry>
</content>
<dyn>
<entry>
<key>FIRSTNAME</key>
<value>john</value>
</entry>
</dyn>
<email>johnblum@flowerpower.com</email>
<encrypt>BdX7CqkmjSivyBgIcZoN4sPVLkx7FaXGiwsO</encrypt>
<notificationId>6464</notificationId>
<random>985A8B992601985A</random>
<senddate>2008-12-12T00:00:00</senddate>
<synchrotype>NOTHING</synchrotype>
<uidkey>EMAIL</uidkey>
</arg0>';

$args = array(new SoapVar($xml, XSD_ANYXML));
$res = $client->__soapCall('sendObject', $args);
return $res;
} catch (SoapFault $e) {
echo "Error: {$e}";
}

echo "<hr>Last Request";
echo "<pre>", htmlspecialchars($client->__getLastRequest()), "</pre>";

how to create soap xml request in php

I have solve my question if any body in future have any problem they this code works for him, please check-

<?php
error_reporting(E_ALL);

define('API_SITEID', $your_siteid);
define('API_USERNAME', $your_uname);
define('API_PASSWORD', $your_pass);
define('API_WSDL', 'http://tripauthority.com/hotel.asmx?WSDL');
ini_set("soap.wsdl_cache_enabled", "0");

$xmlReq = '<ArnRequest>
<Availability DisplayCurrency="USD" SearchTimeout="15">
<HotelAvailability InDate="2014-09-26" OutDate="2014-09-27" Rooms="1" Adults="1" Children="0">
<Hotel HotelID="8800"/>
</HotelAvailability>
</Availability>
</ArnRequest>';
echo '<form action="" method="post">
<strong>XML Request:</strong>
<p>
<textarea style="width:100%;height:400px;" id="xmlReq" name="xmlReq">'.$xmlReq.'</textarea>
</p>
<input type="submit" name="submit" id="submit" value="Test Request">
<input type="hidden" name="avail" id="avail" value="y">
</form>';

if($_POST['avail'] == "y") {
$xmlRes = doSoapRequest((($_POST['xmlReq']) ? $_POST['xmlReq'] : $xmlReq));
echo '<strong>XML Response:</strong>
<p>
<textarea style="width:100%;height:400px;" id="xmlRes" name="xmlRes">'.$xmlRes.'</textarea>
</p>';
}

function doSoapRequest($xmlReq) {
try {
$client = new SoapClient(API_WSDL);
return $client->SubmitRequestRpc(API_SITEID, API_USERNAME, API_PASSWORD, $xmlReq);
} catch(SoapFault $exception) {
return "Fault Code: {$exception->getMessage()}";
}
}

?>

Thanks

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.
    ?>

PHP - SOAP Request | XML/WSDL

I have found the soltuion! :)

<?php
$xml_data = '
<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:Header>
<ThirdPartyTokenHeader xmlns="http://moviestarplanet.com/">
<ThirdPartyToken>8346D304-F85E-4dc1-98EB-033CBEE0217F</ThirdPartyToken>
</ThirdPartyTokenHeader>
</soap:Header>
<soap:Body>
<Login xmlns="http://moviestarplanet.com/">
<username>USER</username>
<password>PASS</password>
</Login>
</soap:Body>
</soap:Envelope>
';

$headers = array(
"POST /WebService/ThirdParty/ThirdPartyService.asmx HTTP/1.1",
"Referer: www.moviestarplanet.fr",
"User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9505 Build/KOT49H)",
"Content-Type: text/xml; charset=utf-8",
"Host: www.moviestarplanet.fr",
"Content-length: ".strlen($xml_data),
"Expect: 100-continue"
);

$url = 'http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

$reply = curl_exec($ch);

echo($reply);
?>


Related Topics



Leave a reply



Submit