2013-05-04 54 views
0

我有以下代碼,看看wolfram阿爾法xml數據,並拉出圍繞在<plaintext>標籤中的答案。它適用於第一個標籤,但不適用於答案之後。代碼不完全解析數據

代碼:

<?php 
$url = "http://api.wolframalpha.com/v2/query?input=what+day+is+today&appid=9QA6R9-VGL4AAURHU&format=plaintext&podtitle=Result"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
ob_start(); 
curl_exec($ch); 
curl_close($ch); 
$contents = ob_get_contents(); 
ob_end_clean(); 
echo $contents; 

下面是答案,我得到:

Monday, May 3, 2011</plaintext> 
    </subpod> 
</pod> 
<sources count='2'> 
    <source url='http://www.wolframalpha.com/sources/AstronomicalDataSourceInformationNotes.html' 
     text='Astronomical data' /> 
    <source url='http://www.wolframalpha.com/sources/PeopleDataSourceInformationNotes.html' 
     text='People data' /> 
</sources> 
</queryresult> 
+0

您好!這對我來說可以。記住要查看源代碼,而不僅僅是瀏覽器顯示的內容。 – 2013-05-04 00:58:26

回答

2

試試這個:

<?php 
$url = "http://api.wolframalpha.com/v2/query?input=what+day+is+today&appid=9QA6R9-VGL4AAURHU&format=plaintext&podtitle=Result"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
ob_start(); 
curl_exec($ch); 
curl_close($ch); 
$contents = ob_get_contents(); 
ob_end_clean(); 

function get($a,$b,$c){ 
    $y = explode($b,$a); 
    $x = explode($c,$y[1]); 
    return $x[0]; 
} 

echo get($contents,'<plaintext>','</plaintext>'); 
?> 
+0

我把那條線放在哪?我不明白它應該去的地方。 – derekshull 2013-05-04 01:05:19

+1

用'echo $ contents;'替換它,這也只是爲了您的觀看樂趣,您已經可以使用這些信息了。它顯示奇怪的唯一原因是因爲您在瀏覽器中查看它。 – 2013-05-04 01:05:35

+0

顯示了整個xml代碼。所有的。 – derekshull 2013-05-04 01:10:00