1

我有一个看起来像这样的 json-feed,我需要使用 cURL 进行解码。

"count":836,
"value":{
"title":"AW-rss",
"description":"Pipes Output",
"link":"http:\/\/pipes.yahoo.com\/pipes\/pipe.info?_id=566903fd393811762dc74aadc701badd",
"pubDate":"Tue, 04 Sep 2012 16:33:30 +0000",
"generator":"http:\/\/pipes.yahoo.com\/pipes\/",
"callback":"",
"items":[
{
"title":"Title",
"description":"Description",
"link":"http://",
"category":"Category",
"pubDate":null,
"guid":"14917809",
"author":"Author",
"y:published":null,
"y:id":{
"permalink":"false",
"value":"14917809"
},
"y:title":"title"
},
//and then it continues like that with several more items.

这是我正在使用的代码,但我一辈子都无法获得 foreach 循环来给我一些实质性的东西。

$query = 'http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=json';

    $ch = curl_init(); // open curl session

    // set curl options
    curl_setopt($ch, CURLOPT_URL, $query);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
    $result = curl_exec($ch); // execute curl session
    $data = json_decode($result);
    curl_close($ch); // close curl session



    **foreach($data['items'] as $item) {
        $guid['guid']}**

如您所见,我正在尝试获取提要中项目的 guid。任何帮助将非常感激。

var_dump($data); 给出:

object(stdClass)#1 (2) { ["count"]=> int(836) ["value"]=> object(stdClass)#2 (7) { ["title"]=> string(6) "AW-rss" ["description"]=> string(12) "Pipes Output" ["link"]=> string(75) "http://pipes.yahoo.com/pipes/pipe.info?_id=566903fd393811762dc74aadc701badd" ["pubDate"]=> string(31) "Tue, 04 Sep 2012 20:34:56 +0000" ["generator"]=> string(29) "http://pipes.yahoo.com/pipes/" ["callback"]=> string(0) "" ["items"]=> array(836) { [0]=> object(stdClass)#3 (10) { ["title"]=> string(68) "Noggrann chefsassistent till välkänt företag i centrala Stockholm" ["description"]=> string(163) "Har du tidigare erfarenhet av arbete som VD-assistent och önskar bli en del av ett av Sveriges största företag? Då är du den som vi söker, ansök redan idag!" ["link"]=> string(122) "http://www.academicwork.se/jobbannons/noggrann-chefsassistent-till-valkant-foretag-i-centrala-stockholm/stockholm/14917809" ["category"]=> string(16) "Assistent- enkel" ["pubDate"]=> NULL ["guid"]=> string(8) "14917809" ["author"]=> string(13) "Academic Work" ["y:published"]=> NULL ["y:id"]=> object(stdClass)#4 (2) { ["permalink"]=> string(5) "false" ["value"]=> string(8) "14917809" } ["y:title"]=> string(68) "Noggrann chefsassistent till välkänt företag i centrala Stockholm" } [1]=> object(stdClass)#5 (10)

等等。

4

1 回答 1

2

json_decode 返回一个对象,因此您需要将其与普通数组稍有不同。

$data = json_decode($result);

$items = $data->{'value'}->{'items'};
$GUID = array();
foreach($items as $obj)
{
  $GUID[] = $obj->{'guid'};
}

var_dump($GUID);

我会注意到,如果有帮助,json_decode 方法可以将返回值转换为数组 (json_decode($result,true))。像这样:

$data = json_decode($result,true);

$items = $data['value']['items'];
$GUID = array();
foreach($items as $obj)
{
  $GUID[] = $obj['title'];
}

var_dump($GUID);
于 2012-09-04T21:39:05.630 回答