我有一个看起来像这样的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-loop给我一些实质性的东西。
$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']}**
正如您所看到的,我正在尝试获取Feed中项目的内容。任何帮助都会非常感激。
的var_dump($数据);给出:
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)
等等。
答案 0 :(得分: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);