I've been working on this for days and I can't seem to get it to work. Does anyone see a problem with the code below?
The result is nothing. I get no response from the service whatsoever. I've confirmed that the webservice, written in .Net, can accept and xml string, I've matched their schema, etc. But I get no response back.
I should also mention that I'm new to using web services with php but have done this in .Net countless times.
Additionally, is there a way to test the webservice .asmx to view any type of error output in php just to see if it's even working on the server side?
<?php
$url = 'https://someurl/someservice.asmx';
$xml = '<?xml version="1.0" encoding="utf-8" ?>
<query>
<version>1.0</version>
<login>userName</login>
<password>password</password>
<keyword>cancer</keyword>
</query>';
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded", "Content-length: ".strlen($xml)));
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'xmlInput='.urlencode($xml));
curl_setopt($c, CURLOPT_TIMEOUT, (int)30);
$xmlResponse = curl_exec($c);
curl_close ($c);
echo 'Output :: '.$xmlResponse;
?>
Have you tried adding the following to your request?
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
try without
<?xml version="1.0" encoding="utf-8" ?>
or whrite
<?xml version=\"1.0\" encoding=\"utf-8\" ?>
Related
Edit
The code given below will work in localhost as it is, if anyone want to copy and try it.The given credentials are valid.
I have a php code which requests data from an API service. An XML request is sent and in response XML data is recieved. I have stored the respose data in a variable $output. When doing echo $output, the details in the XML response is printed. But now I need to parse this response and store the required data in variables. E.g: I need to save $customer_id = value from the <customer_id>12345</customer_id>. I did a thorough google search and tried all the snippets provided by different developers, but no use.
I tried var_dump(simplexml_load_string($output)); and it is returning object(SimpleXMLElement)#1 (0) { }. I even tried converting the XML data to array.
index.php
<?php
$appId ="MFS149250";
$appPass ="5TEBRPCZ";
$brokeCode ="ARN-149250";
$iin = "5011217983";
$xml_data = '<?xml version="1.0" encoding="UTF-8"?>
<NMFIIService>
<service_request>
<appln_id>'.$appId.'</appln_id>
<password>'.$appPass.'</password>
<broker_code>'.$brokeCode.'</broker_code>
<iin>'.$iin.'</iin>
</service_request>
</NMFIIService>';
$URL = "https://uat.nsenmf.com/NMFIITrxnService/NMFTrxnService/IINDETAILS";
$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);
//echo "<textarea>".$output."</textarea>";
echo $output;
var_dump(simplexml_load_string($output));
curl_close($ch);
?>
The following URL is from the FobBugz API documentation:
https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2 (you can find it here)
If I copy and paste the above URL into a web browser I get an XML response. What I would like to do is create a function that returns the XML response as a its result.
I am so stuck, it is simply not working. All I seem to get in response is an empty string. When I use this 'example' on the FogBugz site I get XML telling me that I am NOT logged in.
The function below comes mostly from here: Make a HTTPS request through PHP and get response I have been messing with it for hours without success.
function searchBug(){
$data = "https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2";
//echo $data;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
[EDIT: in response to comment]
The response that I want to receive is:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<error code="3">
<![CDATA[ Not logged in ]]>
</error>
</response>
As that is what is shown in my browser when I paste the URL and press enter.
You can do it like below:-
<?php
function searchBug($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$sXML = searchBug('https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2');
header('Content-Type: text/xml');
echo $sXML;
Output:- http://prntscr.com/f5fj44
I'm using the following function to send data to a particular API.
function api_post($xml) {
$ch = curl_init('http://api.asmx');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml;charset=UTF-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$results = curl_exec($ch);
return $results;
}
The output is
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LoadResponse xmlns="http://api.api.com/">
<LoadResult>
<date>2015-09-18T10_07_51.997</date>
<data><br>⢠bullet1<br>⢠Bullet2</data>
</LoadResult>
</LoadResponse>
</soap:Body>
</soap:Envelope>
The results returned are as expected except that bullet points are returned as â¢and date values as 2015-09-18T10_07_51.997 instead of 2015-09-18T10:07:51.997.
When I test out the same API call with the same XML in Soap UI everything is returned accurately. I'm assuming I have some kind of encoding issue in PHP. How can I resolve?
Depending on the remote encoding, you can use:
return utf8_encode($results);
You can also try
return utf8_decode($results);
NOTE:
If you plan to output utf-8 to a browser, you can also use the following :
<?php
//Teel the browser we'll be outputting UTF-8
header('Content-Type: text/html; charset=UTF-8');
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');
Update based on you comment:
Try setting CURLOPT_ENCODING to "" (empty) and remove CURLOPT_HTTPHEADER, i.e.:
curl_setopt($ch, CURLOPT_ENCODING, "");
I'm having problems submitting a feed to Amazon MWS. I keep receiving the following error: "Invalid query string provided - Keys may not contain <"
This is my code:
$apicall = $this->build_url($function, $params);
$ch = curl_init($apicall);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if ($this->xml) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, (string)$this->xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-MD5: " . base64_encode(md5($this->xml))));
}
else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$info = print_r(curl_getinfo($ch), true);
curl_close($ch);
$apicall is formed on-the-fly and comes in the form:
https://mws.amazonservices.com/Feeds/2009-01-01?ASINList.ASIN.1=B00C5XBAOA&AWSAccessKeyId=***&Action=SubmitFeed&FeedType=_POST_PRODUCT_PRICING_DATA_&MWSAuthToken=***&SellerId=***&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-06-09T09%3A58%3A01.000Z&Version=2009-01-01&Signature=***
(which works fine with other calls to Reports or to Orders)
$this->xml is kept as a "TEXT" field in the MySQL db; this is a sample XML (I added lines to make it readable):
<?xml version="1.0"?>
<AmazonEnvelope xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>***</MerchantIdentifier>
</Header>
<MessageType>Price</MessageType>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Price>
<SKU>***</SKU>
<StandardPrice currency="USD">33.5</StandardPrice>
</Price>
</Message>
</AmazonEnvelope>
I seem to review every single relevant link on the internet and cannot find the answer.
Maybe someone can give me a hint what may go wrong in the above code?
Thanks.
Found the solutions myself (after digging more that a day):
1) Content-MD5 must be calculated in the following way:
base64_encode(md5($this->xml, **true**));
(thanks to this answer: https://sellercentral.amazon.com/forums/message.jspa?messageID=2767745)
2) passing header parameters to cUrl must be one-time operation, that is - all headers have to be passed as an array.
I am trying to get response form my API via CURL, the method needs to get user data via XML and than returns XML with response data.
I get nothing back. API works well, when I try the same request elsewhere than in PHP, it works. My API requires the data to be sent in application/xml Content-Type.
Any ideas what I am doing wrong?
<?php
$request="https://api.mydomain.com/operation/v1/rest/xml/user/authenticate";
$xml_data='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<userCredentials>
<userName>name</userName>
<password>password</password>
</userCredentials>';
$ch = curl_init();
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_URL, $request);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-type: application/xml");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo "<b>Request:</b> $request <br/>";
echo "<b>Response:</b><pre>".$output."</pre>";
?>
As commented, quick guess (lengthy comment):
echo "<b>Response:</b><pre>".$output."</pre>";
This will output the XML unescaped. Your browser will not display anything inside the browser window, but you should be fine to see something when you view the source of the page.