PHP - How to set https headers to soap ws request - php

I'm using SoapUI 5.3.0 to test a SOAP ws request.
I was requested to send user and password via https headers not via soap headers.
It works well when I use this SoapUi tool:
However, when I try to do it from php I always get an authentication error, exactly the same error I get when I use a wrong password on purpose, I've tried several combinations but none of them gave me expected results
code example:
$data['Contrato'] = '123456';
$data['FechaInicio'] = '11/07/2017';
$data['FechaFin'] ='11/07/2017';
$client = new SoapClient( "https://example.com/WebService?WSDL", array(
"exceptions" => 0,
"trace" => 1,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => 'Username:xxx#gmail.com\\n\\r Password:notrealpwd'
),
)),
));
$result = $client->__soapCall('depositos', $data);
Does any of you knows what I am doing wrong?

try with:
$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
"login" => $login, "password" => $password) );

At the end I finished using curl to set the needed headers, that solved it.
<?php
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hub="https://example.com/WebService?WSDL">
<soapenv:Header/>
<soapenv:Body>
<hub:depositos>
<!--Optional:-->
<hub:solicitud>
<Contrato>123456</Contrato>
<FechaInicio>11/07/2017</FechaInicio>
<!--Optional:-->
<FechaFin>11/07/2017</FechaFin>
</hub:solicitud>
</hub:depositos>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: ''",
"Content-length: ".strlen($xml_post_string),
"Username: xxx#gmail.com",
"Password: notrealpwd"
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $ws_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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);

Related

SOAP request working with SOAP UI but not with code

I am trying to execute following WSDL request in PHP. SOAP api does not have any authentication.
When i am running this code using SOAP UI it is working fine. But through code it is not working.
Here is my code:
<?php
$soapUrl = "https://dimasys.plasticentre.be:8443/dimasys/prices/DimasysService.wsdl";
// xml post structure
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins="http://www.infomat.eu/dimasys/insertorderrequest"><soapenv:Header/><soapenv:Body><ins:insertOrderRequest><ins:order><ins:order_no>TEST-DODE 8</ins:order_no><ins:customer><ins:customer_no></ins:customer_no><ins:besteladresnummer>1</ins:besteladresnummer><ins:customer_user_volgnr>0</ins:customer_user_volgnr></ins:customer><ins:email>helpdima#infomat.eu</ins:email><ins:created_dt>2021-02-05</ins:created_dt><ins:firma>PLC</ins:firma><ins:billingAddress><ins:address><ins:company>Test infomat</ins:company><ins:initials></ins:initials><ins:firstname></ins:firstname><ins:prefix></ins:prefix><ins:lastname></ins:lastname><ins:addressline1>Laarstraat</ins:addressline1><ins:addressline2></ins:addressline2><ins:house_number>16</ins:house_number><ins:house_number_addition></ins:house_number_addition><ins:zipcode>2610</ins:zipcode><ins:city>Wilrijk</ins:city><ins:state></ins:state><ins:country><ins:countrycode>BE</ins:countrycode><ins:name>Belgiƫ</ins:name></ins:country><ins:addressnumber></ins:addressnumber></ins:address></ins:billingAddress><ins:shippingAddress><ins:address><ins:company>Test infomat</ins:company><ins:initials></ins:initials><ins:firstname></ins:firstname><ins:prefix></ins:prefix><ins:lastname></ins:lastname><ins:addressline1>Laarstraat</ins:addressline1><ins:addressline2></ins:addressline2><ins:house_number>16</ins:house_number><ins:house_number_addition></ins:house_number_addition><ins:zipcode>2610</ins:zipcode><ins:city>Wilrijk</ins:city><ins:state></ins:state><ins:country><ins:countrycode>BE</ins:countrycode><ins:name>Belgiƫ</ins:name></ins:country><ins:addressnumber></ins:addressnumber></ins:address></ins:shippingAddress><ins:orderLines><ins:order_line><ins:line_id>10</ins:line_id><ins:sku>929740</ins:sku><ins:eancode></ins:eancode><ins:description></ins:description><ins:qty_ordered>2</ins:qty_ordered><ins:regular_price></ins:regular_price><ins:promo_price></ins:promo_price><ins:discount_percentage></ins:discount_percentage><ins:discount_amount></ins:discount_amount><ins:sales_price></ins:sales_price><ins:total_amount></ins:total_amount><ins:vat_rate></ins:vat_rate></ins:order_line></ins:orderLines><ins:shippingCosts>0</ins:shippingCosts><ins:subTotal></ins:subTotal><ins:shippingMethod></ins:shippingMethod><ins:paymentMethod></ins:paymentMethod><ins:paymentReference></ins:paymentReference></ins:order></ins:insertOrderRequest></soapenv:Body></soapenv:Envelope>'; // data from the form, e.g. some ID number
$webService = new SoapClient($soapUrl);
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: https://dimasys.plasticentre.be:8443/dimasys/insertorders",
"Content-length: ".strlen($xml_post_string),
);
$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_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);
$response = curl_exec($ch);
curl_close($ch);
echo "<pre>";var_Dump($response);exit;
?>
I am getting blank response in return. Please help me how i can do it.
I tried SOAP Client as well as php CURL.
Using SoapClient I'm able to connect to the SOAPService when I'm setting the location parameter. This parameter is useful whenever the SOAPService sits behind a proxy and answers with its local IP address. When omitting this parameter I cant connect to the server.
Note that I'm sending dummy data to the Server which results in a SoapFault Server Error.
The created request from the wsdl is sent inside the parameters array.
Hope this is useful as a starting point.
<?php
try {
$wsdlUrl = 'https://dimasys.plasticentre.be:8443/dimasys/prices/DimasysService.wsdl';
$soapOptions = [
'soap_version' => SOAP_1_2,
'location' => 'https://dimasys.plasticentre.be:8443/dimasys/prices/DimasysService.wsdl',
'encoding' => 'utf-8',
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE
];
$client = new SoapClient($wsdlUrl, $soapOptions);
$return = $client->__soapCall('getPrijzen', [
'parameters' => [
'tarieflijst' => 'test',
'factuuradresnummer' => 10,
'leveradresnummer' => 10,
'taal' => 'test',
'muntcode' => 'test',
'artikelen' => [
'artikel' => [
'artikelcode' => '123',
'aantal' => 10
]
]
]
]);
var_dump($return);
} catch(Exception $e) {
var_dump($e);
}

Converting cURL to wp_remote_post not working viz privat24 API

trying to get the info from api by sending xml data to it.
The thing is that it is working great with cURL, but not working via wp_remote_post.
Here is the example of xml data:
<?xml version="1.0" encoding="UTF-8"?>
<request version="1.0">
<merchant>
<id>151880</id>
<signature>23f59a159bbc82</signature>
</merchant>
<data><oper>cmt</oper><wait>60</wait><test>0</test><payment
id="a9c71ad9a291209f8b77d851c4b46f9"><prop name="sd" value="07.11.2019" />
<prop name="ed" value="21.11.2019" /><prop name="card" value="4242424242424242" />
</payment></data>
</request>"
$api_uri = 'https://api.privatbank.ua/p24api/rest_fiz'
cURL working code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: text/xml",
"Content-length: ".strlen($xml),
"Connection: close",
"Access-Control-Allow-Origin: *",
'Accept: text/xml'
));
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)){
echo curl_error($ch);
}else{
curl_close($ch);
}
return $response;
Not working wp_remote_post code:
$result = wp_remote_post($api_uri, array(
'headers' => array(
"Content-Type" => "text/xml",
"Content-length" => strlen($xml),
"Connection" => "close",
"Access-Control-Allow-Origin" => "*",
'Accept" => "text/xml'
),
'method' => 'POST',
'sslverify' => true,
'timeout' => 100,
'body' => $xml
));
$response = $result;
return $response['body'];
Also, it is working through Rest Client as well.
Please help.
Thank you.

XML request is not being sent using wp_remote_post()

appologies in advance. I am trying to send XML request using wp_remote_post().
Im trying to send request to my SMS Provider to send one time OTP.
$url = "http://gate.payvand.tj/xml/";
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<request>
<request-type>9</request-type>
<terminal-id>".$termid."</terminal-id>
<login>".$login."</login>
<password-md5>".$pwd."</password-md5>
<message txn-id=\"".$txn."\" sign=\"".$sign."\">
<to>
<source-address>".$source_address."</source-address>
<destination-address>".$mobile_number."</destination-address>
<data-encoding>1</data-encoding>
<text>".$message."</text>
</to>
</message>
</request>";
$response = wp_remote_post(
$url,
array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'headers' => array(
'Content-Type' => 'text/xml'
),
'body' => array('postdata' => $xml, 'postfield' => 'value'),
'sslverify' => false
)
);
My provider is not recieving this request.
But he is recieving and executing it like this
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<request>
<request-type>9</request-type>
<terminal-id>".$termid."</terminal-id>
<login>".$login."</login>
<password-md5>".$pwd."</password-md5>
<message txn-id=\"".$txn."\" sign=\"".$sign."\">
<to>
<source-address>".$source_address."</source-address>
<destination-address>".$mobile_number."</destination-address>
<data-encoding>1</data-encoding>
<text>".$message."</text>
</to>
</message>
</request>";
$ch = curl_init();
$url = "http://gate.payvand.tj/xml/";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
Could you please explain the mistake in the first snippet?

Soap client request in Php with CURL not working

How can I create php soap request to look like this. I am unable to get the response in Php curl. But fiddler web debugger is working really well with the code.
Here is the raw request:
POST http://195.230.180.189:8280/services/TopupService?wsdl HTTP/0.9
Host: 195.230.180.189:8280
Content-Length: 691
<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:top="http://www.inew-cs.com/mvno/integration/TopupService/">
<soapenv:Header/>
<soapenv:Body>
<top:TopupRequest>
<amount>1</amount>
<amountUnitRelation>1</amountUnitRelation>
<subscriber>
<msisdn>8801701340002</msisdn>
</subscriber>
<source>
<distributorId>PayWell</distributorId>
</source>
<referenceId>12345</referenceId>
<transactionId>09876543</transactionId>
</top:TopupRequest>
</soapenv:Body>
</soapenv:Envelope>
In Php curl request:
$url="http://195.230.180.189:8280/services/TopupService?wsdl";
$xml='<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:top="http://www.inew-cs.com/mvno/integration/TopupService/">
<soapenv:Header/>
<soapenv:Body>
<top:TopupRequest>
<amount>1</amount>
<amountUnitRelation>1</amountUnitRelation>
<subscriber>
<msisdn>8801701340002</msisdn>
</subscriber>
<source>
<distributorId>100</distributorId>
</source>
<referenceId>12345</referenceId>
<transactionId>09876543</transactionId>
</top:TopupRequest>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
echo $result = curl_exec($ch);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
//echo 'Operation completed without any errors';
}
// Close handle
curl_close($ch);
You should not post to that URL. That is not a service endpoint, it is the WSDL, which defines the provided operations.
PHP SoapClient class allows you to build soap requests easily:
$wsdl = 'http://195.230.180.189:8280/services/TopupService?wsdl';
$options = [
'trace' => true,
'cache' => WSDL_CACHE_NONE,
'exceptions' => true
];
$client = new SoapClient($wsdl, $options);
$payload = [
'amount' => 1,
'amountUnitRelation' => 1,
'subscriber' => [
'msisdn' => '8801701340002'
],
'source' => [
'distributorId' => 'PayWell'
],
'referenceId' => '12345',
'transactionId' => '09876543',
];
$response = $client->topup($payload);
After parsing the given wsdl, the $client now has the method topup, as defined by <wsdl:operation>.

php soap:ServerServer was unable to process request. ---> Sequence contains no elements

I need to send an XML as soap request using CURL. I have created xml using
$xml = "<?xml .............." blah blah
That looks 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:Body><extLoginData xmlns='http://www.JOI.com/schemas/ViaSub.WMS/'><ThreePLKey>4dfdf34</ThreePLKey><Login>abv</Login><Password>abc</Password><FacilityID>1ee</FacilityID><CustomerID>xfs</CustomerID></extLoginData><orders xmlns='http://www.JOI.com/schemas/ViaSub.WMS/'><Order><TransInfo><ReferenceNum>Test</ReferenceNum><PONum>12345</PONum></TransInfo><ShipTo><Name></Name><CompanyName>Peter's Test</CompanyName><Address><Address1>7301 Lennox Ave Unit E3</Address1><Address2></Address2><City>Los Angeles</City><State>CA</State><Zip>90010</Zip><Country>US</Country></Address><PhoneNumber1>858-449-8022</PhoneNumber1><EmailAddress1>lshaules#mercatismedia.com</EmailAddress1><CustomerName>Elizabeth Shaules</CustomerName></ShipTo><ShippingInstructions><Carrier>USPS</Carrier><Mode>First Class Mail</Mode><BillingCode>Prepaid</BillingCode></ShippingInstructions><OrderLineItems><OrderLineItem><SKU>947</SKU><Qualifier>XXX</Qualifier><Qty>1</Qty></OrderLineItem></OrderLineItems></Order></orders></soap:Body></soap:Envelope>
And I am using following code to send CURL request
$url = 'http://someurl.com/Contracts.asmx';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'SOAPAction:"http://www.example.com/ViaSub.WMS/CreateOrders"',
'Content-Type: text/xml; charset=utf-8',
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $orderXml);
$result = curl_exec($curl);
But I am getting
soap:ServerServer was unable to process request. ---> Sequence contains no elements
I have searched but haven't found any thing related to it. Can you please let me know what I am doing wrong and how I do it?
This looks like a schema validation error. You don't provide a link to the definition, but a quick search showed me this documentation page. If this is the one you need take a look at the WSDL: https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL
Look for instance the element PalletCount within Order. You see it has minOccurs="1", meaning it's mandatory. But in the xml string you copy there's no such element. That's indeed a sequence with missing elements. (There may be some other schema inconsistencies, have a close look).
Also, consider using the SoapClient PHP class
$WSDL = 'https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL';
$options = [
'trace' => true,
'cache' => WSDL_CACHE_NONE,
'exceptions' => true
];
$client = new SoapClient($WSDL, $options);
$payload = [
'extLoginData' => [
'ThreePLKey' => '4dfdf34',
'Login' => 'abv',
'Password' => 'abc',
'FacilityID' => '1ee',
'CustomerID' => 'xfs'
],
'orders' => [
'Order' => [
// etc...
]
]
]);
$response = $client->CreateOrders($payload);
This way the client handles the schema validation, headers setting (you can set additional headers, though), the namespacing, and the array to xml conversion.
Use this
$xml = "<?xml ..............</soap:Envelope>";
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://www.example.com/ViaSub.WMS/CreateOrders",
"Content-length: ".strlen($xml),
); //SOAPAction: your op URL
$url = 'http://someurl.com/Contracts.asmx';
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

Categories