我使用:http://graph.facebook.com/{user_id or page_id}?fields=cover
浏览器返回:
{
"cover": {
"id": "XXX",
"source": "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/311205_989690200741_1231438675_n.jpg",
"offset_y": 66
},
"id": "XXXXXX"
}
如何将字段source
和offset_y
转换为PHP
个变量?
我的功能:
function cover_img($fb_user_name) {
$cover_data = 'https://graph.facebook.com/'.$fb_user_name.'?fields=cover';
$JSONString = file_get_contents($cover_data);
$parsedJSON = json_decode($JSONString);
echo $source = $parsedJSON->cover->source;
echo $offset_y = $parsedJSON->cover->offset_y;
}
答案 0 :(得分:0)
要在PHP中解析JSON,您需要使用json_decode(returned_facebook_json)
,然后在访问Object时访问数据,例如
$JSONString=file_get_contents('http://graph.facebook.com/{user_id or page_id}?fields=cover');
$parsedJSON = json_decode($JSONString);
echo $source = $parsedJSON->cover->source;
echo $offset_y = $parsedJSON->cover->offset_y;