2010-06-24 220 views
1

我使用curl從soundcloud檢索信息。它提供了大量的信息。但我想過濾它。從curl檢索信息

<?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-urldownloadabletitle信息等,

回答

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代替

+0

感謝這將是非常感謝fule對我整合api – rajanikant 2010-06-24 04:53:01

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>"; 

?> 
+0

感謝它將非常幫助fule對我整合api – rajanikant 2010-06-24 04:52:39