Parsing XML Response with xml schema - php

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);
?>

Related

Get api result from URL using PHP

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

Premature end of file. XML response

I am getting a Premature end of file response to my XML request for following code.
Cant figure out where is the error.
$xml = '<?xml version="1.0" ?>';
$xml .= '<PickUpCityListRQ>';
$xml .= '<Credentials username="'.$api->username.'" password="'.$api->password.'" remoteIp="'.$api->remoteIp.'" />';
$xml .= '<Country>UK</Country>';
$xml .= '</PickUpCityListRQ>';
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?serverName=www.rentalcars.com&xml='.utf8_decode(trim($xml));
$port = 443;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Fail on errors
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 15s
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, '&xml='.$xml); // add POST fields
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
if ($port==443) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml; charset=UTF-8', 'Accept: application/xml; charset=UTF-8'));
$data = curl_exec($ch);
curl_close($ch);
Have tried sending data in post fields as well but did not work.
Response:
<!DOCTYPE DefaultRS SYSTEM 'https://xml.rentalcars.com:443//tj.dtd'><DefaultRS>
<Error id="2">
<Message>Premature end of file.</Message>
</Error>
</DefaultRS>
Any help appreciated.
Thanks
You need to url encode the xml data before you can use it as a query parameter. So the url assignment becomes:
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?
serverName=www.rentalcars.com&xml='.urlencode(utf8_decode(trim($xml)));
Note, I've just wrapped that for readability - that should obviously be on one line.
After trying a lot of variation I finally managed to make it work.
There were two things that were casuing problem.
utf8_decode should be utf8_encode
POSTFIELDS data is supposed to be an array
otherwise parser will throw Premature end of file or not send back anything.

Send POST request with empty response

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.

Can't see XML response from PHP cURL api Call

I'm using a remote API with cURL and querystrings. The issue I'm having is that I can't see the data being returned to me (which is in XML format). To start, here's my code:
$url = 'https://api.somehost.com/GetRequest.do';
$query = 'User=JohnDoe&Password=ABC123&Function=auth';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$curlresponse = curl_exec($ch);
I know the cURL request is working because if I change the password to something incorrect, I get a response back (can see it using print_r($curlresponse) telling me the password is incorrect.
However, when I enter the correct password, I see nothing when I print_r($curlresponse) yet I know I'm receiving the data because a print_r(curl_getinfo($ch)) shows a larghe download_content_length which is the data I'm expecting. The data I'm expecting back is in XML format. I'm just not sure what I'm missing.
Thank you!

POST XML data to url as value portion of key:value pair in post body with PHP/cURL (or anything else)

Ok so here's my situation. I need to POST XML data to a secure URL (ColdFusion web service) and display the returned results (returned as XML data). The requirements are as follows:
Must be a POST request.
Must use XML data string.
The XML data must be the value portion of a name=>value pair. (name can be anything)
The name=>value pair must be in the body of the request.
Content Type header must be "application/x-www-form-urlencoded"
Other than that I know of no other restrictions. I am trying to do this with PHP/cURL but can't seem to get it to work. I am able to establish the connection with the web service fine but I am returning an error that indicates that no post body is being sent. Here's my code:
$url = "https://coldfusion.cfm";
$username = "xxxxx";
$password = "xxxxxxx";
$array = array(
"idata" => "<?xml version='1.0' standalone='yes'?>
<XMLDATA1>
<XMLDATA2>
<XMLDATA3>Query</XMLDATA3>
<XMLDATA4>Platinum</XMLDATA4>
<XMLDATA5>1</XMLDATA5>
</XMLDATA2>
<XMLDATA6>
<XMLDATA7>2440031317</XMLDATA7>
</XMLDATA6>
</XMLDATA1>");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $output;
Can someone please give me an idea what i am doing wrong? If I use REST Console in Chrome I can POST the XML data as a name=>value pair via request parameters in Request Payload options and everything functions as it should. Please HELP!
Your your xml has to be a string sent via post.

Categories