Instagram API搜索带有给定标签的图像/视频

时间:2016-05-12 11:12:18

标签: php instagram instagram-api

我编写了一个脚本,可以从Instagram获取特定标记的图像。 它运作良好,但它只返回我在Instagram帐户上发布的图像,但我也想要其他用户图像。

我尝试了很多,但它只显示我发布的图像。

这是我的剧本

header('Content-type: application/json');

$token = "Access Token";
$query = "cricket";
$api = "https://api.instagram.com/v1/tags/" . $query . "/media/recent?access_token=" . $token;

function get_curl($url)
{
    if (function_exists('curl_init'))
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $output = curl_exec($ch);
    echo curl_error($ch);
    curl_close($ch);
    return $output;
    }
    else
    {
    return file_get_contents($url);
    }
}

$response = get_curl($api);
//print_r($response);
//die();

$images = array();

if ($response)
{
    foreach (json_decode($response)->data as $item)
    {
    $src = $item->images->standard_resolution->url;
    $thumb = $item->images->thumbnail->url;
    $url = $item->link;

    $images[] = array(
        "src" => htmlspecialchars($src),
        "thumb" => htmlspecialchars($thumb),
        "url" => htmlspecialchars($url)
    );
    }
}
print_r(str_replace('\\/', '/', json_encode($images)));
die();

提前致谢

1 个答案:

答案 0 :(得分:2)

首先,Instagram API端点“标签”需要OAuth身份验证。

您可以使用以下网址

查询特定主题标签(在本例中为雪)的结果

速率限制为每小时5000(X-Ratelimit-Limit:5000)

https://api.instagram.com/v1/tags/snowy/media/recent

样本回复

{
  "pagination":  {
    "next_max_tag_id": "1370433362010",
    "deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead",
    "next_max_id": "1370433362010",
    "next_min_id": "1370443976800",
    "min_tag_id": "1370443976800",
    "next_url": "https://api.instagram.com/v1/tags/snowy/media/recent?access_token=40480112.1fb234f.4866541998fd4656a2e2e2beaa5c4bb1&max_tag_id=1370433362010"
  },
  "meta":  {
    "code": 200
  },
  "data":  [
     {
      "attribution": null,
      "tags":  [
        "snowy"
      ],
      "type": "image",
      "location": null,
      "comments":  {
        "count": 0,
        "data":  []
      },
      "filter": null,
      "created_time": "1370418343",
      "link": "http://instagram.com/p/aK1yrGRi3l/",
      "likes":  {
        "count": 1,
        "data":  [
           {
            "username": "iri92lol",
            "profile_picture": "http://images.ak.instagram.com/profiles/profile_404174490_75sq_1370417509.jpg",
            "id": "404174490",
            "full_name": "Iri"
          }
        ]
      },
      "images":  {
        "low_resolution":  {
          "url": "http://distilleryimage1.s3.amazonaws.com/ecf272a2cdb311e2990322000a9f192c_6.jpg",
          "width": 306,
          "height": 306
        },
        "thumbnail":  {
          "url": "http://distilleryimage1.s3.amazonaws.com/ecf272a2cdb311e2990322000a9f192c_5.jpg",
          "width": 150,
          "height": 150
        },
        "standard_resolution":  {
          "url": "http://distilleryimage1.s3.amazonaws.com/ecf272a2cdb311e2990322000a9f192c_7.jpg",
          "width": 612,
          "height": 612
        }
      },
      "users_in_photo":  [],
      "caption":  {
        "created_time": "1370418353",
        "text": "#snowy",
        "from":  {
          "username": "iri92lol",
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_404174490_75sq_1370417509.jpg",
          "id": "404174490",
          "full_name": "Iri"
        },
        "id": "471425773832908504"
      },
      "user_has_liked": false,
      "id": "471425689728724453_404174490",
      "user":  {
        "username": "iri92lol",
        "website": "",
        "profile_picture": "http://images.ak.instagram.com/profiles/profile_404174490_75sq_1370417509.jpg",
        "full_name": "Iri",
        "bio": "",
        "id": "404174490"
      }
    }
}

你可以在这里玩:

https://apigee.com/console/instagram?req=%7B%22resource%22%3A%22get_tags_media_recent%22%2C%22params%22%3A%7B%22query%22%3A%7B%7D%2C%22template%22%3A%7B%22tag-name%22%3A%22snowy%22%7D%2C%22headers%22%3A%7B%7D%2C%22body%22%3A%7B%22attachmentFormat%22%3A%22mime%22%2C%22attachmentContentDisposition%22%3A%22form-data%22%7D%7D%2C%22verb%22%3A%22get%22%7D

您需要将“身份验证”用作OAuth 2,系统会提示您通过Instagram登录。发布您可能需要重新命名“模板”部分中的“标记名称”。

所有与分页相关的数据都可以在响应中的“分页”参数中找到,并使用它的“next_url”来查询下一组结果。

来源:Instagram API to fetch pictures with specific hashtags