从Instagram获取所有带有特定PHP标签的照片
我需要得到一些图片,其中有一个特定的标签使用PHP?从Instagram获取所有带有特定PHP标签的照片,php,hashtag,instagram,Php,Hashtag,Instagram,我需要得到一些图片,其中有一个特定的标签使用PHP? 任何帮助都会非常棒,或者提示?instagram公共API的标记部分可以帮助您做到这一点。这里是我不久前写的另一个示例: <?php // Get class for Instagram // More examples here: https://github.com/cosenary/Instagram-PHP-API require_once 'instagram.class.php';
任何帮助都会非常棒,或者提示?instagram公共API的标记部分可以帮助您做到这一点。这里是我不久前写的另一个示例:
<?php
// Get class for Instagram
// More examples here: https://github.com/cosenary/Instagram-PHP-API
require_once 'instagram.class.php';
// Initialize class with client_id
// Register at http://instagram.com/developer/ and replace client_id with your own
$instagram = new Instagram('CLIENT_ID_HERE');
// Set keyword for #hashtag
$tag = 'KEYWORD HERE';
// Get latest photos according to #hashtag keyword
$media = $instagram->getTagMedia($tag);
// Set number of photos to show
$limit = 5;
// Set height and width for photos
$size = '100';
// Show results
// Using for loop will cause error if there are less photos than the limit
foreach(array_slice($media->data, 0, $limit) as $data)
{
// Show photo
echo '<p><img src="'.$data->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="SOME TEXT HERE"></p>';
}
?>
要获得超过20个,可以使用“加载更多”按钮
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Instagram more button example</title>
<!--
Instagram PHP API class @ Github
https://github.com/cosenary/Instagram-PHP-API
-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
ul {
width: 950px;
}
ul > li {
float: left;
list-style: none;
padding: 4px;
}
#more {
bottom: 8px;
margin-left: 80px;
position: fixed;
font-size: 13px;
font-weight: 700;
line-height: 20px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#more').click(function() {
var tag = $(this).data('tag'),
maxid = $(this).data('maxid');
$.ajax({
type: 'GET',
url: 'ajax.php',
data: {
tag: tag,
max_id: maxid
},
dataType: 'json',
cache: false,
success: function(data) {
// Output data
$.each(data.images, function(i, src) {
$('ul#photos').append('<li><img src="' + src + '"></li>');
});
// Store new maxid
$('#more').data('maxid', data.next_id);
}
});
});
});
</script>
</head>
<body>
<?php
/**
* Instagram PHP API
*/
require_once 'instagram.class.php';
// Initialize class with client_id
// Register at http://instagram.com/developer/ and replace client_id with your own
$instagram = new Instagram('ENTER CLIENT ID HERE');
// Get latest photos according to geolocation for Växjö
// $geo = $instagram->searchMedia(56.8770413, 14.8092744);
$tag = 'sweden';
// Get recently tagged media
$media = $instagram->getTagMedia($tag);
// Display first results in a <ul>
echo '<ul id="photos">';
foreach ($media->data as $data)
{
echo '<li><img src="'.$data->images->thumbnail->url.'"></li>';
}
echo '</ul>';
// Show 'load more' button
echo '<br><button id="more" data-maxid="'.$media->pagination->next_max_id.'" data-tag="'.$tag.'">Load more ...</button>';
?>
</body>
</html>
<?php
/**
* Instagram PHP API
*/
require_once 'instagram.class.php';
// Initialize class for public requests
$instagram = new Instagram('ENTER CLIENT ID HERE');
// Receive AJAX request and create call object
$tag = $_GET['tag'];
$maxID = $_GET['max_id'];
$clientID = $instagram->getApiKey();
$call = new stdClass;
$call->pagination->next_max_id = $maxID;
$call->pagination->next_url = "https://api.instagram.com/v1/tags/{$tag}/media/recent?client_id={$clientID}&max_tag_id={$maxID}";
// Receive new data
$media = $instagram->getTagMedia($tag,$auth=false,array('max_tag_id'=>$maxID));
// Collect everything for json output
$images = array();
foreach ($media->data as $data) {
$images[] = $data->images->thumbnail->url;
}
echo json_encode(array(
'next_id' => $media->pagination->next_max_id,
'images' => $images
));
?>
如果您只需要基于标记显示图像,则不需要包含包装类“instagram.class.php”。因为Instagram API中的媒体和标签端点不需要身份验证。可以使用以下基于curl的函数根据标记检索结果
function callInstagram($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$tag = 'YOUR_TAG_HERE';
$client_id = "YOUR_CLIENT_ID";
$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;
$inst_stream = callInstagram($url);
$results = json_decode($inst_stream, true);
//Now parse through the $results array to display your results...
foreach($results['data'] as $item){
$image_link = $item['images']['low_resolution']['url'];
echo '<img src="'.$image_link.'" />';
}
函数callInstagram($url)
{
$ch=curl_init();
curl_setopt_数组($ch,数组(
CURLOPT_URL=>$URL,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_SSL_VERIFYHOST=>2
));
$result=curl\u exec($ch);
卷曲关闭($ch);
返回$result;
}
$tag='YOUR_tag_HERE';
$client\u id=“您的\u client\u id”;
$url='1https://api.instagram.com/v1/tags/“.$tag.”/media/recent?client_id=”.$client_id;
$inst_stream=callInstagram($url);
$results=json_decode($inst_stream,true);
//现在分析$results数组以显示结果。。。
foreach($results['data']作为$item){
$image_link=$item['images']['low_resolution']['url'];
回声';
}
我已经设置了一个php代码,它可以帮助您在没有基于标签的api的情况下访问Instagram图像
自2015年11月17日起,您必须对用户进行身份验证,才能提出任何请求(即使是“获取一些具有特定标签的图片”)。见:
2015年11月17日当天或之后创建的应用程序:
所有API端点都需要有效的访问令牌。
2015年11月17日之前创建的应用程序:
2016年6月1日前不受新API行为的影响
这使得2016年6月1日之前给出的所有答案都不再有用。你知道有什么方法可以超过20的限制吗?@Staysee请看下面的答案。@kexxcream我正在运行你的API,我想获取TagMedia的标签#thesouthfarm。问题是,我只得到我自己的图像与该标签。不是我的朋友。有什么限制吗?@kexxcream很抱歉!发现用户有私人档案,因此我无法获取照片!:)使用最新版本的Instagram PHP API包装器,您需要将限制传递给getTagMedia()方法:$Instagram->getTagMedia($tag,5)
然后将foreach行替换为foreach($media->data as$data){
(另外,现在加载包装器的简单方法是使用-参见包装器的Github页面上的示例)这很好!+1如果oy uhave time,你能看看我遇到的类似问题吗?似乎有人能更快地回答,所以我想问题已经解决了。是的,显然。无论如何,谢谢!正是我要搜索的!谢谢Kexxcream。还有一个问题。显示的图片数量一直从13张更改为18张。如何修复这个问题?顺便说一句,很奇怪,图片大小不一样。有时我会看到非正方形的图片!但是很好的脚本!喜欢它!谢谢你,不需要破解核心。只需设置$instagram->setClientID('你的客户端ID'),而不用编辑instagram.class.php
第一个URL似乎坏了。这一个有效:@Shahar更新,谢谢。instagram API看起来很疯狂。他们真的希望你从每个访问者那里获得身份验证,只是为了获得一个公共提要吗?你不需要对用户进行身份验证。你的客户端id就足够了。如果你的应用程序没有身份验证,它将需要用户身份验证代表用户的ething(评论、喜欢、浏览用户的提要等)@SimonEast的意图是你根本不使用API来实现这些目的。他们希望你进入生态系统,而不是从生态系统中提取内容。我注意到它只抓取20张图片。这20张是最新的图片吗?如何获取更多?这是最新的20张。如果你查看URL,它有“媒体/最新”。根据Instagram API,这是将按日期时间降序。@Gursharan Singh您好,我有带下划线的标签,我正在使用此代码,除了带下划线的标签外,其他所有标签都可以使用。但您真的看到所有图片吗?我在沙盒模式下,只能看到accesstoken所有者的图片?我如何在android应用程序usin中从Instagram获取帖子g各种标签?但2017年你可以得到“获取一些有特定标签的图片”,我仍然可以在没有访问令牌的情况下获取数据。例如:我如何在我的android应用程序中使用各种标签获取帖子?太棒了!这正是我所需要的。用NodeJS(ES6)重新编写的代码编写了一个要点:这是伟大的工作@MaximMazurok
function callInstagram($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$tag = 'YOUR_TAG_HERE';
$client_id = "YOUR_CLIENT_ID";
$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;
$inst_stream = callInstagram($url);
$results = json_decode($inst_stream, true);
//Now parse through the $results array to display your results...
foreach($results['data'] as $item){
$image_link = $item['images']['low_resolution']['url'];
echo '<img src="'.$image_link.'" />';
}