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.
Related
Thank you all, I have solved this and wrote my own answer on this question down below. Thank you all for trying
I have a simple (I hope) question as I am a complete newbie.
So I have 2 php files output.php and input.php.
output.php is sending an XML to input.php via cURL like so:
$myXML = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$myXML .= '<request>
<message>Hello StackOverflow</message>
<params>
<name>John</name>
<lname>Smith</lname>
</params>
</request>';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/curltest/input.php');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $myXML);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
'Content-type: text/xml; charset=UTF-8',
'Expect: '
)
);
$curl_response= curl_exec($curl);
Currently the contents of input.php look like this
<?php
// Yes, this is all
The problem is I don't know at all how to get and parse the XML data in input.php and take data out of that XML for use in my input.php script.
$_POST is an empty array in input.php, but the XML surely reaches input.php because readfile('php://input'); inside of input.php gives out the received XML.
I have read about DOMElement but it was kind of unclear to me how to use it in my specific case. I also did a search on Google and in SO also, but didn't succeed in finding exactly what I need.
If someone could tell me how exactly is this done it would be great. Or at least point me in the right direction, I'm at a loss here.
P.S. I can't change anything in output.php. And I will gladly provide any additional information if needed.
Simple XML is a really good library for XML parsing/reading/writing.
http://www.php.net/manual/en/book.simplexml.php
PHP:
$file = new SimpleXMLElement($file);
echo "<b>Testing:</b> {$file->testing} <br/>";
XML:
<document>
<testing>Value</testing>
</document>
You can use Simple XML both from files and variables.
You can find more information about it here: http://www.php.net/manual/en/simplexmlelement.construct.php
you need to change
curl_setopt($ch, CURLOPT_POSTFIELDS, $myXML);
to
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('xml' => $myXML)));
then in your input.php file you can get the data using $_POST['xml'];
so in your input.php then do
$xmldata = new SimpleXMLElement($_POST['xml']);
I've managed to solve it by getting the sent XML string into a variable in the input.php like so:
$xml_data = new SimpleXMLElement(file_get_contents('php://input'));
var_dump($xml_data);
Now I may do whatever I want with it. Thank you all for trying, you're all amazing!
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.
<?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);
I want to get data from XML API output from Vimeo.
In Vimeo if we load this URL: http://vimeo.com/api/v2/video/30055721.xml the video ID is 30055721, it will output the XML data in the browser (single-line XML chunk):
<?xml version="1.0" encoding="UTF-8"?><videos><video><id>30055721</id><title>[MV]I-ny(아이니) 뮤직비디오</title><description>눈부신 가을 하늘을 닮은 목소리의 주인공 '아이니(i-ny)', <br /> 그녀의 이름을 노래하다.</description><url>http://vimeo.com/30055721</url><upload_date>2011-10-04 22:34:19</upload_date><mobile_url>http://vimeo.com/m/30055721</mobile_url><thumbnail_small>http://b.vimeocdn.com/ts/201/671/201671639_100.jpg</thumbnail_small><thumbnail_medium>http://b.vimeocdn.com/ts/201/671/201671639_200.jpg</thumbnail_medium><thumbnail_large>http://b.vimeocdn.com/ts/201/671/201671639_640.jpg</thumbnail_large><user_id>2991448</user_id><user_name>Deviljoon</user_name><user_url>http://vimeo.com/user2991448</user_url><user_portrait_small>http://b.vimeocdn.com/ps/217/387/2173872_30.jpg</user_portrait_small><user_portrait_medium>http://b.vimeocdn.com/ps/217/387/2173872_75.jpg</user_portrait_medium><user_portrait_large>http://b.vimeocdn.com/ps/217/387/2173872_100.jpg</user_portrait_large><user_portrait_huge>http://b.vimeocdn.com/ps/217/387/2173872_300.jpg</user_portrait_huge><stats_number_of_likes>3</stats_number_of_likes><stats_number_of_plays>542</stats_number_of_plays><stats_number_of_comments>0</stats_number_of_comments><duration>235</duration><width>1280</width><height>720</height><tags>I-ny, 아이니, 뮤직비디오, music video, MV, kpop, k-pop, 550d</tags><embed_privacy>anywhere</embed_privacy></video></videos>
But I want to retrieve data in the XML field dynamically to show it in my webpage.
Check out this article for a complete run through:
http://ditio.net/2008/06/19/using-php-curl-to-read-rss-feed-xml/
This should give you a good idea about how to fetch the XML contents into your PHP script, then parse the contents of the XML into your PHP. You will need to make some adaptations to the process of parsing the feed, specific to the vimeo output, but you should be able to do this simply by having a play.
e.g. the below will output the ID.
$ch = curl_init("http://vimeo.com/api/v2/video/30055728.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$xml= new SimpleXmlElement($data, LIBXML_NOCDATA);
echo "<strong>".$xml->video->id."</strong>";
Once $xml has been established, simply change $xml->video->id to whichever node you want (crucually the 'id' section').
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.