我正在使用curl从soundcloud中检索信息。它提供了大量的信息。但我想过滤它。
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://api.soundcloud.com/tracks ');
curl_exec($curl_handle);
curl_close($curl_handle);
?>
如何过滤来自stream-url
,downloadable
,title
等的信息。
答案 0 :(得分:2)
有许多工具可用于提取您想要的内容。
您正在下载的流是一个xml文件,因此您可以将其输出通过php或直接在命令行上传输到某个解析器。
您可以在此处查看php的内置XML解析器:http://php.net/manual/en/book.xml.php
编辑以下是一个示例用法
<?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";
}
?>
我最终使用的是SimpleXML
答案 1 :(得分:0)
<?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>";
?>