How to create simple SOAP request? - php

I tried to create a SOAP request but could not succeed.
SOAP WADL url is http://www.mobipost.com.au/httpapi/Messaging.asmx?WSDL
AND request should be like:
<?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:Header>
<AuthenticationHeader xmlns="http://messaging.mobipostapi.thirdscreen.com.au/">
<UserName>SAMPLE_USER</UserName>
<Password>SAMPLE_PASS</Password>
</AuthenticationHeader>
</soap:Header>
<soap:Body>
<SendSMSToContacts xmlns="http://messaging.mobipostapi.thirdscreen.com.au/">
<oSMS>
<MessageText>THIS IS MESSAGE</MessageText>
</oSMS>
<ContactIDs>
<int>123456789</int>
<int>987654321</int>
</ContactIDs>
</SendSMSToContacts>
</soap:Body>
</soap:Envelope>
I tried:
$url = 'http://www.mobipost.com.au/httpapi/Messaging.asmx?WSDL';
$client = new SoapClient($url);
$result = $client->AuthenticationHeader(array('UserName' => 'SAMPLE_USER','Password' => 'SAMPLE_PASS'));
$result = $client->SendSMSToContacts(array('MessageText' => 'THIS IS MESSAGE'));
$result = $client->ContactIDs(array('123456789', '987654321'));
But it shows Error:
Fatal error: Uncaught SoapFault exception: [Client] Function ("AuthenticationHeader") is not a valid method for this service in D:\xampp\htdocs\globalmobile\send_message_v2.php:26 Stack trace: #0 D:\xampp\htdocs\globalmobile\send_message_v2.php(26): SoapClient->__call('AuthenticationH...', Array) #1 D:\xampp\htdocs\globalmobile\send_message_v2.php(26): SoapClient->AuthenticationHeader(Array) #2 {main} thrown in D:\xampp\htdocs\globalmobile\send_message_v2.php on line 26
Can you please help me to create valid SOAP request to communicate with WSDL server.
Thank you in advance.

There are must be only one request, you send three.
You must create something like this:
1) Set header for authentication:
$auth = $auth = new SOAPAuth('USERNAME', 'PASSWORD');
$header = new SOAPHeader('urn:example.org/auth', 'AuthenticationHeader', $auth);
$client->__setSoapHeaders($header);
2) Create create request with message text and contacts.
$result = $client->SendSMSToContacts(array("MessageText" => "some text", "contactIDs" => array(123456789, 123456789));
P.S. For the debugging create client with:
$client = new SoapClient($url, array('trace' => 1,
'exceptions' => 1,));
and after sending request, look on request that was sended and resopnse on it:
var_dump("REQUEST=", $client->__getLastRequest());
var_dump("RESPONSE=", $client->__getLastResponse());

Related

SOAP-ERROR: Encoding: object has no 'ACCION' property

I have a PHP script which calls a web service created in SAP, I already validated it with soapui5 and it works fine, but PHP throws the following message:
SOAP-ERROR: Encoding: object has no 'ACCION' property
PHP code :
$url = 'https://www.ceramicaitalia.com/wssap/zwebservice1.wsdl';
$client = new SoapClient($url);
echo PHP_EOL;
$xmlr = new SimpleXMLElement("<ZWS_WEBSERVICE1></ZWS_WEBSERVICE1>");
$xmlr->addChild('login', '***');
$xmlr->addChild('password', '******');
$xmlr->addChild('ACCION', '/');
$params = new stdClass();
$params->xml = $xmlr->asXML();
$client->ZWS_WEBSERVICE1($params);
$result = $client->ZWS_WEBSERVICE1('RTA');
print_r($result);
Test in soapui5 :
Request :
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:urn="urn:sap-com:document:sap:rfc:functions">
<soap:Header/>
<soap:Body>
<urn:ZWS_WEBSERVICE1>
<ACCION>/</ACCION>
</urn:ZWS_WEBSERVICE1>
</soap:Body>
</soap:Envelope>
Response :
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<n0:ZWS_WEBSERVICE1Response xmlns:n0="urn:sap-com:document:sap:rfc:functions">
<RTA>1.5</RTA>
</n0:ZWS_WEBSERVICE1Response>
</env:Body>
</env:Envelope>
You don't need to set root element. You can to xml as array param. Library will auto set matched elements. If you have multiple element with same name but different namespace you can check SoapVar. Working sample is below.
<?php
$url = 'https://www.ceramicaitalia.com/wssap/zwebservice1.wsdl';
$client = new SoapClient($url, ['trace' => true]);
$params = ['ACCION' => '/'];
$client->ZWS_WEBSERVICE1($params);
$result = $client->ZWS_WEBSERVICE1('RTA');
print_r($result);
?>

PHP SOAP Header for Authentication Issue

I have the below SOAP XML, Im trying to access the vehicle details with the soap header authentication. I have tried the below code. I think, Im missing something on this. Can u help
SOAPAction: "http://abcddetails.org/getVehicleDetails"
<?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:Header>
<UserIdentifierSoapHeaderIn xmlns="http://abcddetails.org/">
<UserName>string</UserName>
<Password>string</Password>
</UserIdentifierSoapHeaderIn>
</soap:Header>
<soap:Body>
<getVehicleDetails xmlns="http://abcddetails.org/">
<request>
<SystemCode>int</SystemCode>
<UserID>string</UserID>
<PlateInfo>
<PlateNo>long</PlateNo>
<PlateOrgNo>long</PlateOrgNo>
<PlateColorCode>int</PlateColorCode>
</PlateInfo>
<ChassisNo>string</ChassisNo>
</request>
</getVehicleDetails>
</soap:Body>
PHP code along with the SOAP Header, I have created as the below.
<?php
$wsdl = "http://abcddetails.org/InspectionServices.asmx?WSDL";
$client = new SoapClient($wsdl, array('trace'=>1)); // The trace param will show you errors stack
$auth = array(
'Username'=>'XXXXX',
'Password'=>'XXXXX',
);
$header = new SOAPHeader($wsdl, 'UserIdentifierSoapHeaderIn', $auth);
$client->__setSoapHeaders($header);
// web service input params
$request_param = array(
"SystemCode" => 4,
"UserID" => "TEST",
"ChassisNo" => '1N4AL3A9XHC214925'
);
$responce_param = null;
try
{
$responce_param = $client->getVehicleDetails($request_param);
//$responce_param = $client->call("webservice_methode_name", $request_param); // Alternative way to call soap method
}
catch (Exception $e)
{
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
print_r($responce_param);
?>
Can u guide if anything I have written wrong here in this.
You can use the __soapCall method like this:
$result = $client->__soapCall('webserviceMethodeName', ['parameters' => $params]);
In your case a soap action would be invoked like this:
$responce_param = $client->__soapCall('getVehicleDetails', ['parameters' => $request_param]);
Read more

php Encoding: Violation of encoding rules

I have a PHP script that is doing a SOAP request to the server. PHP errors out with
SOAP-ERROR: Encoding: Violation of encoding rules
Initial instinct is there is an issue with my request. I took a network capture of the request and sent it to the web server via curl and it seems to have come back OK. The request that is sent by PHP soap and curl is the same which is:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Integrics/Enswitch/API" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:download_music_file><username xsi:type="xsd:string">dovid</username><password xsi:type="xsd:string">passsowrd</password><music xsi:type="xsd:int">45</music><file xsi:type="xsd:int">1</file><checking xsi:type="xsd:int">0</checking></ns1:download_music_file></SOAP-ENV:Body></SOAP-ENV:Envelope>
The response is:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><download_music_fileResponse xmlns="http://www.example.com/Integrics/Enswitch/API"><gensym><name xsi:type="xsd:string">music_45_1.wav</name><data xsi:type="xsd:base64Binary">BASE 64 REMOVED</data><mimetype xsi:type="xsd:string">audio/x-wav</mimetype></gensym></download_music_fileResponse></soap:Body></soap:Envelope>
The php code that does the quest is:
<?PHP
ini_set("soap.wsdl_cache_enabled", "0");
$uid = 'dovid';
$pass = 'password';
$func = 'download_music_file';
$soap = new SoapClient("enswitch_3.9.wsdl", array('trace' => true, 'exceptions' => false, 'keep_alive' => false, 'connection_timeout' => 15));
$result1 = $soap->$func($uid, $pass, 45, 1,0);
if(isset($soap->__soap_fault)){
echo 'There was an error connecting via SOAP. Below is the error:';
print_r($soap->__soap_fault->faultstring);
}
?>

Order XML to soap client

I have to make a script that sends a order (XML) to a SOAP client server (this way the supplier can handle the order)
I tried everything but it doesn't work.
Does anybody know what I'm doing wrong?
This is what I have so far:
<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.datacontract.org/2004/07/Centric.CS.Trade.Standard.WS.SalesOrderService.Contract.Request" xmlns:ns2="http://centric.eu/services/CS/Trade/Standard/WS/">
<SOAP-ENV:Body>
<ns2:LoadSalesOrder>
<ns2:request>
<ns1:SalesOrderRequests>
<ns1:SalesOrderRequest>
<ns1:DeliveryAddress/>
<ns1:DeliveryAddress1>teststraat</ns1:DeliveryAddress1>
<ns1:DeliveryCountry>NL</ns1:DeliveryCountry>
<ns1:DeliveryEmail>test#test.nl</ns1:DeliveryEmail>
<ns1:DeliveryFetchCarrier>true</ns1:DeliveryFetchCarrier>
<ns1:DeliveryFetchDeliveryMode>true</ns1:DeliveryFetchDeliveryMode>
<ns1:DeliveryHouseNo>1</ns1:DeliveryHouseNo>
<ns1:DeliveryMunicipality>ALKMAAR</ns1:DeliveryMunicipality>
<ns1:DeliveryName>Test naam</ns1:DeliveryName>
<ns1:DeliveryPermanent>false</ns1:DeliveryPermanent>
<ns1:DeliveryPhone>06-12345678</ns1:DeliveryPhone>
<ns1:DeliveryPostalCode>1200 RT</ns1:DeliveryPostalCode>
<ns1:Division>AGU_NL</ns1:Division>
<ns1:Key>02</ns1:Key>
<ns1:Language>NL</ns1:Language>
<ns1:Login>7440475</ns1:Login>
<ns1:OrderCustomer>7440475</ns1:OrderCustomer>
<ns1:OrderLines>
<ns1:SalesOrderRequest.OrderLine>
<ns1:Item>113504</ns1:Item>
<ns1:Line>10</ns1:Line>
<ns1:OrderQuantityBU>1.0000</ns1:OrderQuantityBU>
</ns1:SalesOrderRequest.OrderLine>
</ns1:OrderLines>
<ns1:OrderType>100</ns1:OrderType>
<ns1:ReferenceExternal>100000001</ns1:ReferenceExternal>
</ns1:SalesOrderRequest>
</ns1:SalesOrderRequests>
</ns2:request>
</ns2:LoadSalesOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
$wsdl = 'https://ws.abcb2b.eu/Centric/CS/Trade/cstest/SalesOrderService.svc?wsdl';
$client = new SoapClient($wsdl, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'cache_ttl' => 86400,
'trace' => true,
'exceptions' => true,
));
$xmlVar = new SoapVar($xmlstr, XSD_ANYXML);
$client->LoadSalesOrder($xmlstr);
?>
Fatal error: Uncaught SoapFault exception: [s:Client] The creator of this fault did not specify a Reason. in C:\wamp\soap_request.php:102 Stack trace: #0 C:\wamp\www\soap_request.php(102): SoapClient->__call('LoadSalesOrder', Array) #1 C:\wamp\www\soap_request.php(102): SoapClient->LoadSalesOrder('

PHP sending XML with soap returns unknown rerror

I'm trying to send user registrations with soap to another server. I'm creating an xml with DOMdocument and than after saveXML I'm running the soap which should return an xml with registration id plus all the data I sent in my xml.
But the Soap returns unknown error. exactly this: stdClass Object ( [RegisztracioInsResult] => stdClass Object ( [any] => 5Unknown error ) )
and this is how I send my xml.
/*xml creation with DOMdocument*/
$xml = saveXML();
$url = 'http://mx.biopont.com/services/Vision.asmx?wsdl';
$trace = '1';
$client = new SoapClient($url, array('trace' => $trace, "exceptions" => 0, 'features' => SOAP_SINGLE_ELEMENT_ARRAYS));
$params = $client->RegisztracioIns(array('xml' => $xml));
$print_r($params);
If I click on the description of the RegisztracioIns service at this URL http://mx.biopont.com/services/Vision.asmx it shows me this:
POST /services/Vision.asmx HTTP/1.1
Host: mx.biopont.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://mx.biopont.com/services/RegisztracioIns"
<?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>
<RegisztracioIns xmlns="http://mx.biopont.com/services/">
<xml>string</xml>
</RegisztracioIns>
</soap:Body>
</soap:Envelope>
According to this I think I'm doing the upload correctly but maybe not I don't have much experience with soap.
Is there anything I'm missing? I also tried to save the xml to my server an than get the contents with file_get_contents(). but the result was the same.
You should be able to do something like this:
$res = $client->__soapCall( 'RegisztracioIns', array('xml'=>'my string to send'));
To have the wsdl wrap 'my string to send' in the proper tags.
You are doing something similar, but I dont think the wsdl is actually wrapping the string you are trying to pass, and passing nothing instead, resulting in unknown error.
You can examine the outgoing xml using $client->__getLastRequest();.
(Also, you have a small typo in your code on the last line should be print_r($params);.)
Failing that you could try to write the xml yourself using SoapVar() and setting the type to XSD_ANYXML.
It seems cleaner to me when the wsdl wraps everything for you, but its better than banging your head against the wall until it does.
I made an attempt to do just that with your wsdl. Give this a try:
$wsdl = "http://mx.biopont.com/services/Vision.asmx?wsdl";
$client = new SoapClient($wsdl, array( 'soap_version' => SOAP_1_1,
'trace' => true,
));
try {
$xml = "<RegisztracioIns xmlns='http://mx.biopont.com/services/'>
<xml>string</xml>
</RegisztracioIns>";
$args= array(new SoapVar($xml, XSD_ANYXML));
$res = $client->__soapCall( 'RegisztracioIns', $args );
var_dump($res);
} catch (SoapFault $e) {
echo "Error: {$e}";
}
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
I can't exactly read the response I am getting with that given that it is Hungarian (I think?). So let me know if that works for you.

Categories