Instagram的file_get_contents返回空响应 [英] file_get_contents for Instagram return empty response

查看:142
本文介绍了Instagram的file_get_contents返回空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I use https://www.instagram.com/instagram/?__a=1 in my browser I get a nice json return ;))

在PHP脚本中使用它

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

返回始终为空!!!

我做错了什么?如果我使用Curl

What I did wrong ? I have the same problem if I use Curl

感谢您的帮助...

推荐答案

我还尝试从那里获取json这样的json响应中的数据(或不再具有curl im信息),但是过了一段时间,Instagram阻止了您的IP地址并返回空响应。

I also tried to scrape data from there json response like that (or with curl im not sure anymore), but after a time Instagram blocks your IP address and return an empty response.

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

below the curl function I used

  $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

这篇关于Instagram的file_get_contents返回空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆