代码未完全解析数据

时间:2013-05-04 00:56:38

标签: php parsing curl

我有以下代码查看wolfram alpha 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>

1 个答案:

答案 0 :(得分: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>');
?>