tags:

views:

603

answers:

1

Hello all,

I am not well-versed in HTTP requests.

How can I, using PHP, grab and parse the information from this XML page, using a GET HTTP Request?

http://heywatch.com/encoded_video/1850189.xml

Basically, this is a video that was encoded, and I am trying to pull some of the values from the recently encoded video. More specifically, I am trying to pull the video length, so that I can add it to my db.

I currently am using:

require_once "HTTP/Request.php";

$req =& new HTTP_Request("http://heywatch.com/encoded_video/1850189.xml");
$req->setBasicAuth("myusername", "mypassword");
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->clearPostData();
if (!PEAR::isError($req->sendRequest())) {
 $response2 = $req->getResponseBody();
} else {
   $response2 = "";
}

echo $response1;
echo $response2;

But, I'm not sure how I would parse each piece of information. The HTTP GET Request information can be found here, but they don't provide any examples.

A nudge in the right direction on how to do this (using PEAR or cURL, whatever might be more efficient) would be appreciated.

Thanks!

A: 

You can do a request in curl using the following code

$url = "http://heywatch.com/encoded_video/1850189.xml";

$request = curl_init();
curl_setopt($request,CURLOPT_URL,$url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($request);
curl_close($request);

The text will then be held in $response. I would recommend using simple XML to parse the XML document to pick out the information you need. It's very easy to use. If you don't want to use curl at all, and you don't have to, you can load the document directly with SimpleXML using simplexml_load_file, and passing in the URL.

Kibbee
Thanks for the reply, Kibbee. Using simplexml_load_file, (which works great) I am able to parse basic aspects, for example, using "echo $xml->id[0];" works fine in parsing the id. However, the video-length is inside an array (using attributes?), and looks like: [video] => SimpleXMLElement Object ( [@attributes] => Array ( [bitrate] => 800 [width] => 640 [fps] => 0.0 [codec] => h264 [length] => 34 [height] => 480 [container] => flv [stream] => 0.0 [aspect] => 1.33 ) ) How can I parse the 'length' from this?
Dodinas
Nevermind, I was looking for this: echo $xml->specs[0]->video[0]->attributes()->length;Thanks again!
Dodinas