Parsing XML response from CURL - confused - php

Using CURL, I send an xml file to a server and receive a 200 response and reciprocal XML file from said server.
$response = curl_exec($session);
So far so good. If I do print_r($response) I see that the url i need is indeed inside $response.
The question i have is how do i parse it out? I try variations of the following but nothing seems to work:
$xml = new SimpleXMLElement($response);
Pointer in the right direction would be great.
Thanks!

You need to set the right curl options. It looks like the header information is being included in the response data, which of course makes the response invalid XML. You can see the curl options here:
http://www.php.net/manual/en/function.curl-setopt.php
You'll want to turn off including the headers like this:
curl_setopt($ch, CURLOPT_HEADER, false);

You need use the following structure:
$xml = new SimpleXMLElement($response);
echo $xml->movie[0]->plot;
//Or
$xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
Where movie is a node from yor xml structure.

Related

SimpleXML: can't fetch data from XML file

I am trying to use the DELL API to fetch warranty information from our products. DELL provides an XML file that I want to extract the end of warranty date from.
Location of the XML file: https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXXXX1&apikey=YYYYYYYYYYYYYYYYY
[Note: svctags and apikey obfuscated]
Code:
$xml = simplexml_load_file("https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXXXXX1&apikey=YYYYYYYYYYYYYYYYYY");
print_r($xml->GetAssetWarrantyResult->{'a:Response'}->{'a:DellAsset'}->{'a:Warranties'}->{'a:Warranty'}->{'a:EndDate'});
This is not working. I get an empty page when executing this code. Can anybody explain why? Thanks in advance!
you can used curl, For ex:
$url = "https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=1RP22W1&apikey=1adecee8a60444738f280aad1cd87d0e"
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
$response = curl_exec($ch);
In case you wanted more.
get url: https://api.dell.com/support/v2/assetinfo/warranty/tags.json?svctags="
+tag+"&apikey=1adecee8a60444738f280aad1cd87d0e
Then parse the response in (javascript).
In python you could also read each line page.readlines() and use encode=json.loads(data) to start reading the result like a dictionary.

Page not being converted into xml format

I am grabbing a page and then converting it into an xml format, the function im using is below
public function getXML($url){
$ch = curl_init();
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
return $xml;
}
print_r($curl->getXML("http://www.amazon.co.uk/gp/offer-listing/0292783760/ref=tmm_pap_new_olp_sr?ie=UTF8&condition=used"));
After trying different urls nothing is returned, the page loads fine so the problem is with the line $xml = simplexml_load_string($response);
What could be wrong with this code?
Not understanding exactly what you're up to, it looks like you're trying to scrape the Amazon web page? If I pull up that URL in my browser, it's not listed as XHTML in the headers or document itself--I suspect it's not. I don't think simplexml can handle that.
(Does CURL do the conversion to XML for you? I don't think so but I'm not a master of all things CURL. If so, it might be an incompatability between CURL's output and what simplxml--which is fairly limited--will take in).
You might try working with DOMDocument instead, although my PHP could be a bit out of date--there may be better utilities these days.
A quick googling brought up this tutorial
<?php
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
?>
I don't think this is a complete answer, but it was a bit much for a comment; so take it with a grain of salt and a healthy serving of doubt. I hope it inspires some ideas.

Parsing XML feed in PHP

<?php
$url='http://bart.gov/dev/eta/bart_eta.xml';
$c = curl_init($url);
curl_setopt($c, CURLOPT_MUTE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$rawXML = curl_exec($c);
curl_close($c);
$fixedupXML = htmlspecialchars($rawXML);
foreach($fixedupXML->eta-> as $eta) {
echo $eta->destination;
}
?>
As a way to get introduced to PHP, I've decided to parse BART's XML feed and display it on my webpage. I managed (also via this site) to be able to fetch the data and preserve the XML tags. However, when I try to output the XML data, using what I found to be the simplest method, nothing happens.
foreach($fixedupXML->eta as $eta){
echo $eta->destination;
}
Am I not getting the nested elements right in the foreach loop?
Here is the BART XML feed http://www.bart.gov/dev/eta/bart_eta.xml
Thanks!
You may want to look at simplexml, which is a fantastic and really simple way to work with XML.
Here's a great example:
$xml = simplexml_load_file('http://bart.gov/dev/eta/bart_eta.xml');
Then you can run a print_r on $xml to see it's contents:
print_r($xml);
And you should be able to work with it from there :)
If you still need to use curl to get the feed data for some reason, you can feed the XML into simplexml like this:
$xml = simplexml_load_string($rawXML);

Help with XML response from PHP

I am new to PHP, and I am using it to POST data from an iPhone. I have written a basic code to get data from the iPhone, post it to the php script on my server, and then using PHP, send that data to another webserver. However, the webserver is returning a response in XML, and since I am a newbie to PHP, I need help with it.
My code to send data:
<?php
$ch = curl_init("http://api.online-convert.com/queue-insert");
$count = $_POST['count'];
$request["queue"] = file_get_contents($count);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);
echo $response;
?>
I know I need to parse the XML response, but I have no idea how to do that. The XML response would be something like this:
<?xml version="1.0" encoding="utf-8"?>
<queue-answer>
<status>
<code>0</code>
<message>Successfully inserted job into queue.</message>
</status>
<params>
<downloadUrl>http://www.online-convert.com/result/07d6c1491bb5929acd71c531122d2906</downloadUrl>
<hash>07d6c1491bb5929acd71c531122d2906</hash>
</params>
</queue-answer>
You're probably looking for SimpleXML or DOMDocument.
SimpleXML
To load data from a string into an object: simplexml_load_string().
DOMDocument
To create one from a string: DOMDocument->loadXML().
Read about SimpleXML: http://php.net/manual/en/book.simplexml.php
Google is your friend!
The built-in PHP XML Parser is your best bet.

Get value from remote page and store as local variable

I have a page on a remote server that returns a single xml value
<?xml version="1.0" ?><Tracker>12345</Tracker>
How do I go about getting the value 12345 or whatever is in the tag into a PHP variable?
I know I can't do something as simple as:
<?php
$var = http://www.www.com/file.php;
echo $var; //12345
?>
I'm new to the entire idea of creating a web service - I think that's what this should be.?.
You have to go in two steps :
Get the XML string from the remote server
with curl, for instance
or with file_get_contents
and, then, parse that XML to extract the data
with simplexml_load_string for instance
Note that those two steps can be merged as one, using simplexml_load_file, if the configuration of your server allows you to do that.
Once you have that XML in a PHP variable, using SimpleXML, it's quite easy to get the value you want. For instance :
$string = '<?xml version="1.0" ?><Tracker>12345</Tracker>';
$xml = simplexml_load_string($string);
echo (string)$xml;
Will get you
12345
And getting the content of that XML from the remote URL can be as simple as :
$string = file_get_contents('http://your-remote-url');
And if you can do that, you can also use that remote URL directly with SimpleXML :
$xml = simplexml_load_file('http://your-remote-url');
echo (string)$xml;
But this will only work if allow_url_fopen is enabled in your server's configuration.
If allow_url_fopen is disabled on your server, you can get the XML from the remote server using curl ; something like this should do the trick, for instance :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://your-remote-url");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$string = curl_exec($ch);
curl_close($ch);
And, then, you can still use simplexml_load_string, as I did in my first example.
(If using curl, take a look at the documentation of curl_setopt : there are several options that might interest your -- for instance, to specify timeouts)
I don't know PHP that good, but:
Download the contents of the XML file - read it for the remote location
Parse the XML
store the value of the parsed element in a varianble
$xml = simplexml_load_file('test.xml');
$var = $xml[0];

Categories