【问题标题】:Get Instagram feed with Curl使用 Curl 获取 Instagram 提要
【发布时间】:2020-08-25 17:26:59
【问题描述】:

我正在尝试使用 Instagram API 获取 Instagram 图片。这是我使用的代码:

    $url = "https://www.instagram.com/'.$username.'/?__a=1";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result = curl_exec($ch);
    curl_close($ch);
    $insta = json_decode($result, true);

    $instagram_photos = $insta["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"];
    $user = $insta["graphql"]["user"]["username"];
    $name = $insta["graphql"]["user"]["full_name"];
    $bio = $insta["graphql"]["user"]["biography"];
    $profilepic = $insta["graphql"]["user"]["profile_pic_url_hd"];

在我的 Mamp 上显示来自 url 的图片和数据,但在我的生产站点上不会显示任何数据。当我 var_dump $result 变量时,会显示一个空字符串“”。当我 var_dump $url 变量时,Instagram 会正确显示用户名。

【问题讨论】:

    标签: php json curl instagram


    【解决方案1】:

    这似乎是错误的:

    $url = "https://www.instagram.com/'.$username.'/?__a=1";
    

    试试这个:

    $url = "https://www.instagram.com/{$username}/?__a=1";
    

    Instagram 很可能会通过 curl/cli/php 阻止此类请求,或者它会将您的请求转发到登录页面..

    此外,在您的 PHP curl 请求中,您不会进行任何完整性检查。例如,Curl 作为 HTTP 代码返回的内容:

    "Curl returned: ".curl_getinfo($ch, CURLINFO_HTTP_CODE);
    

    检查curl_getinfo

    您也不会检查 JSON 是否没有通过 json_last_error 出错。这些是故障排除的基础知识..

    一旦您知道其中每个返回的内容,您将能够更好地对代码进行故障排除。

    【讨论】:

    • 无结果,变量$result为空
    • 如果你运行:echo file_get_contents("https://www.instagram.com/{$username}/?__a=1"); 会得到什么?
    • 同样从您的生产服务器,通过 CLI,如果您运行:curl -i "https://www.instagram.com/_your_username_here_/?__a=1" 并添加您的用户名作为测试,您会得到什么 HTTP 代码?
    • 使用 file_get_contents 我从 Instagram 获取登录页面,只显示 Instagram 的图标。
    • curl 返回:0 并且 json_last_error 给出一个 int(4)
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多