How to Make a PHP Soap Call Using the Soapclient Class

How do you make a SoapCall using the PHP Soap class client

This question has been solved. The answer was in the body provided in Postman.

<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>
<GetCourseInformation xmlns="http://someurl.com/irmpublic">
<RQ> <----- Notice the RQ here
<Credentials>
<LogonID>SomeLogin</LogonID>
<Password>SomPass</Password>
<DataPath>SomePath</DataPath>
<DatabaseID>SomeId</DatabaseID>
</Credentials>
<CourseNumber></CourseNumber>
<CourseID></CourseID>
<StartDate>2019-12-20T18:13:00</StartDate>
<EndDate>2025-12-29T18:13:00</EndDate>
</RQ>
</GetCourseInformation>
</soap:Body>
</soap:Envelope>

The RQ was never provided so it didn't know how to read the provided parameter. To fix this we simply have to change this:

    $params = array(
"Credentials" => array(
"LogonID" => "SomeLogin",
"Password" => "SomPass",
"DataPath" => "SomePath",
"DatabaseID" => "SomeId",
),
"CourseNumber" => "",
"CourseID" => "",
"StartDate" => "2019-12-05T18:13:00",
"EndDate" => "2025-12-29T18:13:00",
);

To this:

    $params = array(
"RQ" => array(
"Credentials" => array(
"LogonID" => "SomeLogin",
"Password" => "SomPass",
"DataPath" => "SomePath",
"DatabaseID" => "SomeId",
),
"CourseNumber" => "",
"CourseID" => "",
"StartDate" => "2019-12-05T18:13:00",
"EndDate" => "2025-12-29T18:13:00",
)
);

This was a very specific question but I hope this helps someone in the future.

PHP SOAP call issue using the SoapClient class

When structures are as complex as:

<s:element name="inwardProcess">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="xmlInput">
<s:complexType mixed="true">
<s:sequence>
<s:any/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>

and response like

<s:element name="inwardProcessResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="inwardProcessResult">
<s:complexType mixed="true">
<s:sequence>
<s:any/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>

You have to create a class for each structure. In this case, it would be:

class xmlInput
{
public $any = null;
public function __construct($any)
{
$this->any = $any;
}
}

class inwardProcess
{
public $xmlInput = null;
public function __construct($xmlInput)
{
$this->xmlInput = $xmlInput;
}
}
class inwardProcessResponse
{
public $inwardProcessResult = null;
public function __construct($inwardProcessResult)
{
$this->inwardProcessResult = $inwardProcessResult;
}
}

and finally call ...

$xmlInput = new xmlInput('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
$inwardProcess = new inwardProcess($xmlInput);
$soap_options = array(
'trace' => 1, // traces let us look at the actual SOAP messages later
'exceptions' => 1 );
$url = "<WSDL URL>";
$client = new SoapClient($url, $soap_options);
try {
$result = $client->__soapCall("inwardProcess", array($inwardProcess));
echo htmlentities($result->inwardProcessResult->any);
} catch (SOAPFault $f) {
echo "-1";
}

Worked!

Cannot get response from PHP Soapclient

First of all you need to enable Soap extention from php.ini. Restart Apache. Then check phpinfo() to ensure extension is loaded.

$option = array('trace' => 1, 'keep_alive' => true,'connection_timeout' => 60);// add options here
$client = new SoapClient("https://swea.riksbank.se/sweaWS/wsdl/sweaWS_ssl.wsdl", $option);

$params = array (
"year" => 2010,
"month" => 4,
"languageid" => 1 //language id may differ.
);

// you can directly call function from wsdl.
$response = $client->getAnnualAverageExchangeRates(array($params));

var_dump($response);

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>";

Making a Soap Call to Given WSDL

You should definitively use a WSDL to php generator as I think you're not sending the request as it should be.

So using a WSDL to php generator, you will know exactly how to send the request and its associated parameters using PHP objects and the native PHP SoapClient class without having to deal directly with it.

I advise you to try the PackageGenerator project.

Failed to call XML Soap using PHP SoapClient

I have found a way to get the error message of the xml.

$xml = simplexml_load_string($result);
$faultstring = (array) $xml->xpath('//faultstring')[0];
$detail = (array) $xml->xpath('//detail')[0];

return response()->json(['faultstring' => $faultstring[0], 'detail' => $detail[0]]);

This will then return:

{"faultstring":"Authentication Failure","detail":"Invalid Credentials"}

This may not be the best solution, but for now this works for me.

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