如何处理Zend_Rest_Client结果?

时间:2011-05-06 20:27:47

标签: zend-framework rest

我正在尝试使用Zend_Rest_Client从last.fm进行搜索。

我该如何处理回复?如何从响应中获取值?

object(Zend_Rest_Client_Result)[226]
  protected '_sxml' => 
    object(SimpleXMLElement)[228]
      public '@attributes' => 
        array
          'status' => string 'ok' (length=2)
      public 'results' => 
        object(SimpleXMLElement)[229]
          public '@attributes' => 
            array
              ...
          public 'trackmatches' => 
            object(SimpleXMLElement)[230]
              ...
  protected '_errstr' => null

如何在轨道匹配上循环?我尝试的一切都返回null。

3 个答案:

答案 0 :(得分:1)

$results = $object->getIterator();
foreach($result as $result) {
   ...
}

上面的代码将起到魔力。

答案 1 :(得分:1)

在得到不是SimpleXMLElement对象的东西之前,你必须保持解除引用。尝试打印SimpleXMLElement对象不起作用。

$results = $object->getIterator();
foreach($results->results->trackmatches as $t) {
    echo $t->sometagname;
}

答案 2 :(得分:0)

我无法用Zend做到这一点。看起来像Zend_Http这样无用的类。我不得不使用gool'ol SPL:

    $url = $this->host . '?method=track.search';
    $url .= '&api_key=' . $this->apikey;
    $url .= '&track=' . urlencode($value);

    try {
        $xmlstr = file_get_contents($url);
        $xml = new SimpleXMLElement($xmlstr);
        //var_dump($xml->results->trackmatches);
        return $xml->results->trackmatches;
    } catch (Exception $e) {
        echo '<h4>url = ' . $url . '</h4>';
        var_dump($e);
    }