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?
Related
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.
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);
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);
I have an API which says all requests are made as POST requests with data in XML format, and it gives me a sample XML data:
<?xml version="1.0" ?>
<Request>
<SystemName>SomeSystemName</SystemName>
<Client>SomeClientID</Client>
<Method action="SomeAction">MethodParams</Method>
</Request>
Now I use curl to do it, like in this function:
function curl_post_xml($url, $xml, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $xml,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
And in $xml I put that XML string from above. Am I doing it right? Because I heard that when using POST, the data must be in key-value format, but the API doesn't say anything about which variable should I assign XML string to.
Am I doing it right? Because I heard that when using POST, the data must be in key-value format,
Yes, you are right, you have to specify that you are sending an XML that's all.
CURLOPT_HTTPHEADER => array("Content-Type: application/xml"),
// or text/xml instead of application/xml
You do not need to put the $xml under a key. Just pass it as you are doing, its fine.
This works for me:
<?php
$xml_data ='<Request>
<SystemName>SomeSystemName</SystemName>
<Client>SomeClientID</Client>
<Method action="SomeAction">MethodParams</Method>
</Request>';
$URL = "https://www.yourwebserver.com/path/";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
I am calling the discoverAndAdd method of intuit.
According to the specification mentioned here.
I am getting the Authorization headers using the below code:
IntuitAggCatHelpers::GetOAuthTokens(&$oauth_token, &$oauth_token_secret);
$signatures = array('consumer_key' => OAUTH_CONSUMER_KEY,
'shared_secret' => OAUTH_SHARED_SECRET,
'oauth_token' => $oauth_token,
'oauth_secret' => $oauth_token_secret);
$oauthObject = new OAuthSimple();
$oauthObject->reset();
$name='institutions';
$result = $oauthObject->sign(array(
'path' => FINANCIAL_FEED_URL . 'v1/' . $name.'/100000/logins',
'parameters' => array('oauth_signature_method' => 'HMAC-SHA1',
'Host' => FINANCIAL_FEED_HOST),
'signatures' => $signatures));
And then I am calling the discoverAndAdd method using the below code:
$xmlStr='<InstitutionLogin xmlns="http://schema.intuit.com/platform/fdatafeed/institutionlogin/v1">
<credentials>
<credential>
<name>Banking Userid</name>
<value>demo</value>
</credential>
<credential>
<name>Banking Password</name>
<value>go</value>
</credential>
</credentials>
</InstitutionLogin>';
define('XML_PAYLOAD', $xmlStr);
define('XML_POST_URL', $result['signed_url'];
/**
* Initialize handle and set options
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/xml',
'Content-length: ' . strlen(XML_PAYLOAD)
));
$result = curl_exec($ch);
It is supposed to return some challenge headers like this question
but I am getting the below headers :
HTTP/1_1_401_Code:ApplicationAuthenticationFailed,Type:SYSTEM
Date:_Tue,_03_Dec_2013_08:10:17_GMT
intuit_tid:_gw-bb793220-51cd-420b-8502-c4674db06611
Content-Type:_text/plain
Content-Length:_0
The status code tells that there is some problem in authentication but the authentication token is working fine for GetAll method.