【问题标题】:How to echo json object from url as a string如何将 url 中的 json 对象作为字符串回显
【发布时间】:2016-11-11 19:17:57
【问题描述】:

我用这个从url获取json:

$json_string = file_get_contents('https://www.instagram.com/testuser/?__a=1');
$parsed_json = json_decode($json_string, true);

var_dump($parsed_json);

我得到 json 响应:

    {"user": 
 {"username": "testuser", "has_blocked_viewer": false, "follows":
 {"count": 94}, "requested_by_viewer": false, "followed_by":
 {"count": 3}, "country_block": null, "has_requested_viewer": false,
               "external_url_linkshimmed": null, "follows_viewer": false,
               "profile_pic_url_hd": "https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg",
               "profile_pic_url": "https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg",
               "is_private": true, "full_name": null, "media":
 {"count": 0, "page_info":
 {"has_previous_page": false, "start_cursor": null, 
  "end_cursor": null, "has_next_page": false},
  "nodes": []},
  "blocked_by_viewer": false,
  "followed_by_viewer": false, "is_verified": false,
  "id": "3409891", "biography": null, "external_url": null}
}

如何将“profile_pic_url_hd”作为字符串回显?

【问题讨论】:

  • 您确定网址正确吗?如果你回显 $json_string,你会得到什么吗?
  • 你不能真正打印一个数组。 var_dump($parsed_json) 看看你得到了什么。
  • 如果您知道由于下载 https 而收到错误 ssl(尚未安装证书),请更改此问题,或者您可以删除此问题
  • 我只是编辑问题

标签: php json


【解决方案1】:

使用json_decodetrue 作为第二个参数,你得到一个关联数组,你不能printecho 数组。 试试var_dumpprint_r

$json_string = file_get_contents('https://www.instagram.com/username/?__a=1');
$parsed_json = json_decode($json_string, true);
print_r($parsed_json);
var_dump($parsed_json);

还要确保 URL 正确。

【讨论】:

    【解决方案2】:

    确保检查错误/异常。在下文中,我假设返回的 json 响应中存在 'user' 和 'profile_pic_url'(返回的 json 响应是哈希的哈希)。

    <?php
    $json_string = file_get_contents('https://www.instagram.com/testuser/?__a=1');
    $parsed_json = json_decode($json_string, true);
    print_r($parsed_json{'user'}{'profile_pic_url'});
    ?>
    

    回应:

    https://scontent-frt3-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多