【问题标题】:file_get_contents for Instagram return empty responseInstagram 的 file_get_contents 返回空响应
【发布时间】:2020-06-12 12:14:54
【问题描述】:

当我在浏览器中使用 https://www.instagram.com/instagram/?__a=1 时,我得到了一个不错的 json 返回 ;))

在 PHP 脚本中使用它时

$response = file_get_contents('https://www.instagram.com/instagram/?__a=1');
$user = json_decode($response);

返回总是空的!!!

我做错了什么?如果我使用 Curl,我也会遇到同样的问题

感谢您的帮助...

【问题讨论】:

  • 但就我而言,file_get_contents 和 curl 都可以正常工作,您可以在 phpfiddle.org 上尝试相同的代码以确保
  • 对我来说很好。
  • 是的...空响应!
  • var_dump($user) 显示了什么?
  • u_mulder:什么都没有!

标签: php browser instagram file-get-contents


【解决方案1】:

我也尝试过从那里抓取 json 响应中的数据(或者 curl 我不确定了),但一段时间后 Instagram 会阻止您的 IP 地址并返回一个空响应。

编辑:在我使用的 curl 函数下方

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  $server_output = curl_exec($ch);
  $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close ($ch);

  if ($response_code == 200) {
    $insta_json = json_decode($server_output, true);

    if (is_array($insta_json) && isset( $insta_json['graphql']['user'] ) ) {

      $user = $insta_json['graphql']['user'];

      $data_array['user'] = [
          'name'        => isset($user['full_name']) ? $user['full_name'] : '',
          'description' => isset($user['biography']) ? $user['biography'] : '',
          'follow'      => isset($user['edge_follow']['count'])       ? $user['edge_follow']['count']       : '',
          'followed_by' => isset($user['edge_followed_by']['count'])  ? $user['edge_followed_by']['count']  : '',
          'profile_pic' => isset($user['profile_pic_url'])            ? $user['profile_pic_url']            : '',
      ];
      $data_array['images'] = [];

      if (isset($user['edge_owner_to_timeline_media']['edges'])) {

        $images = $user['edge_owner_to_timeline_media']['edges'];

        foreach ($images as $image) {

          $imageData = isset($image['node']) ? $image['node'] : false;

          if ($imageData) {

            $data_array['images'][] = [
                'liked'     => isset($imageData['edge_liked_by']['count'])          ? $imageData['edge_liked_by']['count']          : 0,
                'comment'   => isset($imageData['edge_media_to_comment']['count'])  ? $imageData['edge_media_to_comment']['count']  : 0,
                'image'     => isset($imageData['display_url'])                     ? $imageData['display_url']                     : '',
                'thumb'     => isset($imageData['thumbnail_resources'][2])          ? $imageData['thumbnail_resources'][2]['src']   : '',
                'shortcode' => isset($imageData['shortcode'])                       ? $imageData['shortcode']                       : '',
            ];
          }
        }
      } // End if isset edge_owner_to_timeline_media
    }// End check if user exist in array
  }// End check status code 200

【讨论】:

  • Baracuda078:感谢您的尝试。这很奇怪,因为在浏览器中使用 Instagram url(对我来说)我得到了回应。如果我的 IP 被阻止,浏览器应该什么也不返回!
  • 您是否还随请求发送了有效的用户代理?你是在你的电脑上运行还是在虚拟主机上运行?
  • Baracuda078 : 在 webhost 上运行,如果我使用 file_get_contents 我不需要用户代理
  • 那么您的网络主机的 IP 可能被阻止,函数 file_get_contents() 确实不需要用户代理,但 instagram 会检查您的用户代理以验证您的普通用户
  • Baracuda078 :我只是将我的 IP 切换到移动 IP(不同)和相同的东西!我检查了一个新的浏览器......同样的问题!嗯,这是个谜!
猜你喜欢
  • 1970-01-01
  • 2014-10-10
  • 2021-09-15
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 2023-03-17
相关资源
最近更新 更多