PHP - 解析 Facebook 帖子 JSON
Posted
技术标签:
【中文标题】PHP - 解析 Facebook 帖子 JSON【英文标题】:PHP - Parse Facebook Post JSON 【发布时间】:2015-06-17 05:01:44 【问题描述】:我真的很难从 Facebook 的 Graph API 解析 JSON 数据。我正在使用file_get_contents
和json_decode
来解码对象
$json = file_get_contents($facebook_url);
$obj = json_decode($json, true);
这里是一些示例 JSON:
"data": [
"place":
"id": "134972803193847",
"name": "University of Southern California",
"location":
"city": "Los Angeles",
"country": "United States",
"latitude": 34.021142052439,
"longitude": -118.28519155575,
"state": "CA",
"street": "3551 Trousdale Pkwy",
"zip": "90089"
,
"picture": "https://fbcdn-vthumb-a.akamaihd.net/hvthumb-ak-xtf1/v/t15.0-10/xxxxxxxx",
"id": "xxxxxxxxxx",
"created_time": "2015-06-12T02:26:04+0000"
,
"place":
"id": "74286767824",
"name": "Los Angeles International Airport (LAX)",
"location":
"city": "Los Angeles",
"country": "United States",
"latitude": 33.943998965589,
"longitude": -118.402533192,
"state": "CA",
"street": "1 World Way",
"zip": "90045"
,
"picture": "https://scontent.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/xxxxxxx",
"id": "xxxxxxxx12321",
"created_time": "2015-06-06T22:57:49+0000"
,
],
"paging":
"previous": "https://graph.facebook.com/v2.3/wfefwfwefwe",
"next": "https://graph.facebook.com/v2.3/fweewfwef"
我尝试了各种解决方案,但echo
一直在打印“Array”
我正在尝试为每个帖子获取一些字段,例如 id、姓名、城市、国家/地区、纬度、经度、州、created_time、图片。
我试过这个,它只是打印了很多次“Array”。我知道这是一个简单的 JSON 解析/php 问题。
foreach($obj['data'] as $chunk)
foreach($chunk['place'] as $place)
$fbid = $place['id'];
$name = $place['name'];
$location = $place['location'];
$city = $location['city'];
$country = $location['country'];
$latitude = $location['latitude'];
$longitude = $location['longitude'];
$state = $location['state'];
$tuple = array($fbid, $name, $city, $country, $latitude, $longitude, $state);
echo $tuple . "\n";
【问题讨论】:
【参考方案1】:Live Demo
你的输出是一个对象。所以你不能通过[
访问对象。
试试这个:
foreach($obj->data as $chunk)
$fbid = $chunk->place->id;
$name = $chunk->place->name;
$location = $chunk->place->location;
$city = $location->city;
$country = $location->country;
$latitude = $location->latitude;
$longitude = $location->longitude;
$state = $location->state;
$tuple = array($fbid, $name, $city, $country, $latitude, $longitude, $state);
print_r( $tuple);
输出:
Array
(
[0] => 134972803193847
[1] => University of Southern California
[2] => Los Angeles
[3] => United States
[4] => 34.021142052439
[5] => -118.28519155575
[6] => CA
)
Array
(
[0] => 74286767824
[1] => Los Angeles International Airport (LAX)
[2] => Los Angeles
[3] => United States
[4] => 33.943998965589
[5] => -118.402533192
[6] => CA
)
当你需要打印一个数组时,你不能使用echo
。您应该使用print_r
或var_dump
。
【讨论】:
这段代码给了我一个警告,说我将不正确的对象传递给 foreach 循环:/【参考方案2】:echo
打印 Array()
因为您将数组转换为字符串。
要以人类可读的格式打印数组,请使用print_r($obj);
,然后打印第一个位置的 id,例如,使用 echo $obj['data'][0]['place']['id'];
或遍历所有位置并打印每个 id 使用
foreach ($obj['data'] as $a)
echo $a['place']['id'];
您可以在 PHP 手册页上找到有关 print_r 的更多信息。 http://php.net/manual/en/function.print-r.php
【讨论】:
以上是关于PHP - 解析 Facebook 帖子 JSON的主要内容,如果未能解决你的问题,请参考以下文章
PHP - 仅使用 Facebook 图表从 Facebook 页面获取帖子数据