代码之家  ›  专栏  ›  技术社区  ›  benhowdle89

用PHP从Yahoo BOSS API输出XML

  •  0
  • benhowdle89  · 技术社区  · 15 年前

    到目前为止我设法得到了这个代码:

        <?php
    //Gather data and prepare query
    $thequery = urlencode($_GET['s']);
    $yhost = 'http://boss.yahooapis.com';
    $apikey = 'xxxxxxxxxxxxxxx';
    $url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
    //Get the results
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    $results = new SimpleXmlElement($data, LIBXML_NOCDATA);
    //echo the results
    foreach ($results->resultset_news->result as $theresult) {
    echo '<a href="'.$theresult->clickurl.'">'.$theresult->title.'</a><br/>';
    echo $theresult->abstract.'<br/>';
    echo '<small><i>'.$theresult->dispurl.'</i></small><br/>';
    echo '<br/><br/>';
    }
    

    那么我到底该如何输出实际的XML而不是HTML呢?

    1 回复  |  直到 11 年前
        1
  •  1
  •   Robin    15 年前

    试过这个:

    echo $results->asXML();
    

    ?