【问题标题】:Decoding json string from facebook graph api explorer in php从 php 中的 facebook graph api explorer 解码 json 字符串
【发布时间】:2013-07-18 15:00:49
【问题描述】:

我正在使用图形 api 将我的 facebook 应用用户的个人资料图片设置为指定的宽度和高度。

网址: http://graph.facebook.com/'.$userid.'/picture?width=180&height=220

这将在 json 中返回一些内容,例如 { “数据”: { "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/c0.0.553.676/s320x320/998591_136374463234627_573810314_n.jpg", “宽度”:262, “身高”:320, “is_silhouette”:假 } }

我想知道如何在 php 中对其进行解码,以及如何在返回的 json 字符串中获取“url”。感谢您的帮助。 注意:我将url存储在php中的一个变量中,然后使用url从jpeg(GD库)中创建imagecreate,然后使用该图像并将其与另一个图像合并。

【问题讨论】:

标签: php json facebook-graph-api gd


【解决方案1】:

使用json_decode函数(http://php.net/manual/en/function.json-decode.php)

它接受json字符串作为参数并返回数组或对象

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$obj = json_decode($json);
$array = json_decode($json, true);

然后,访问类似的值

echo $obj->a;
echo $array['a'];

两者都会输出

1

在您的情况下,您可以通过这种方式访问​​网址

$obj = json_decode($your_fb_result);
echo $obj->data->url;

$array = json_decode($your_fb_result, true);
echo $array['data']['url'];

具体到你的情况,

$response = file_get_contents('http://graph.facebook.com/'.$userid.'/picture?width=180&height=220&redirect=false');
$array = json_decode($response, true);
echo $array['data']['url'];

http://php.net/manual/en/function.file-get-contents.php

【讨论】:

  • 这里是代码:$userprofpicurl='graph.facebook.com/'.$userid.'/…'; $array = json_decode($userprofpicurl, true);回声 $array['data']['url'];它似乎不起作用! :(
  • 您首先必须获取该 URL 的输出。 file_get_contents 可以解决问题。查看更新的答案
  • 我检查了您的 URL,它返回的是图像,而不是 JSON。查看stackoverflow.com/questions/2070603/… 了解将二进制数据显示为图像的方法,或者只是在 IMG 标记中的 SRC 属性中传递 URL
  • 这是图片 URL 本身。试试 graph.facebook.com/SOMEUSERID/picture?width=180&height=220'>
  • 如果我改用这个网址会怎样:graph.facebook.com/100005862221464/…
猜你喜欢
  • 1970-01-01
  • 2017-03-29
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多