开发者

retrieving information from curl

I am retrieving information from soundcloud using curl. it gives lot of information. but 开发者_JAVA百科I want to filter it.

<?php
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://api.soundcloud.com/tracks ');
    curl_exec($curl_handle);
    curl_close($curl_handle);
?>

how can I filter information coming from it like stream-url, downloadable, title etc.


There are a number of tools for extracting what you want.

The stream you're downloading is an xml file, so you can pipe the output of that to some parser, either in php or directly on the commandline.

You can see the builtin XML parser for php here: http://php.net/manual/en/book.xml.php

EDIT Here's an example usage

<?php
// Download the Data
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://api.soundcloud.com/tracks ');
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
$xml_data = curl_exec($curl_handle);
curl_close($curl_handle);

//Parse it
$xml = simplexml_load_string($xml_data);

foreach ($xml->track as $track) {
    print "{$track->title}\n";
    print "\tStream URL: {$track->{'stream-url'}}\n";
}

?>

I ended up using SimpleXML instead


<?php

$url = 'http://api.soundcloud.com/tracks';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $body, $data);
xml_parser_free($parser);

print "<h1>The XML as a relatively flat PHP data structure</h1>";
print "<pre>";
print htmlentities($body);
print "</pre>";
print "<hr />";
print "<h1>The Raw XML Data</h1>";
print "<pre>";
print htmlentities(print_r($data, true));
print "</pre>";
print "<pre>";

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜