0

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

1 回答 1

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>');
?>
于 2013-05-04T01:00:30.247 回答