如何获取用户的 Instagram 提要
Posted
技术标签:
【中文标题】如何获取用户的 Instagram 提要【英文标题】:How to get a user's Instagram feed 【发布时间】:2011-06-10 19:29:16 【问题描述】:我想使用 php 获取用户的 Instagram 提要。我已经注册了一个 Instagram 开发者帐户并尝试提取用户的信息和照片,但响应不稳定。有时我会收到回复,有时我会不断收到错误消息:access_token 丢失。是否有通过用户名获取用户照片提要的可靠示例?
理想情况下,我希望它像这样简单:
$instagram = new Instagram();
$photos = $instagram->getPhotos("username-goes-here");
Instagram 是一个处理所有请求的类。任何帮助或方向表示赞赏。谢谢!
【问题讨论】:
Instagram API 发生了很大变化,其他答案不再起作用。我发现以下分步教程对于检索 Instagram 提要 codeofaninja.com/2015/01/display-instagram-feed-website.html 非常有用 【参考方案1】:试试这个,
<?php
function fetchData($url)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
$result = fetchData("https://api.instagram.com/v1/users/ID-GOES-HERE/media/recent/?access_token=TOKEN-GOES-HERE");
$result = json_decode($result);
foreach ($result->data as $post)
// Do something with this data.
?>
希望对您有所帮助。
【讨论】:
这太棒了。也可能对某些人有帮助:获取 OAuth 令牌:jelled.com/instagram/access-token 从用户名获取 ID:jelled.com/instagram/lookup-user-id 它适用于我,但仅适用于生产环境。由于某种原因,localhost 请求为空。 为了能够在沙盒环境中对其进行测试,您需要在 Instagram 中将测试用户添加为沙盒用户。 注意:试图获取非对象的属性 由于沙盒模式,它只会显示 20。您需要进行直播才能展示更多内容。【参考方案2】:我这样做了:
<?php
function fetchData($url)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
$result = fetchData("https://api.instagram.com/v1/users/USER ID HERE/media/recent/?access_token=ACCES TOKEN HERE&count=14");
$result = json_decode($result);
foreach ($result->data as $post)
if(empty($post->caption->text))
// Do Nothing
else
echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
<img src="'.$post->images->low_resolution->url.'" />
<div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>';
?>
【讨论】:
【参考方案3】:根据我在互联网上和这个页面上看到的内容,我在下面创建了一个 Instagram 类(非常简单,仅用于提取提要等)。
class Instagram
public static $result;
public static $display_size = 'thumbnail'; // you can choose between "low_resolution", "thumbnail" and "standard_resolution"
public static $access_token = "DEFAULTACCESSTOKEN"; // default access token, optional
public static $count = 10;
public static function fetch($url)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
function __construct($Token=null)
if(!empty($Token))
self::$access_token = $Token;
// Remove from memory -- not sure if really needed.
$Token = null;
unset($Token);
self::$result = json_decode(self::fetch("https://api.instagram.com/v1/users/self/media/recent?count=" . self::$count . "&access_token=" . self::$access_token), true);
$Instagram = new Instagram('ACCESSTOKENIFCHANGEDORNULLOREMPTY');
foreach ($Instagram::$result->data as $photo)
$img = $photo->images->$Instagram::$display_size;
【讨论】:
【参考方案4】:更新:2017 年 6 月 15 日 - Instagram 更改了终点,以下内容不再起作用。
由于没有经过批准的应用程序就无法再获取随机用户供稿,因此我想出了如何使用非官方 API 获取它:
#!/bin/bash
instagram_user_id=25025320
count=12
csrftoken=$(curl --head -k https://www.instagram.com/ 2>&1 | grep -Po "^Set-Cookie: csrftoken=\K(.*?)(?=;)")
curl "https://www.instagram.com/query/" -H "cookie: csrftoken=$csrftoken;" -H "x-csrftoken: $csrftoken" -H "referer: https://www.instagram.com/" --data "q=ig_user($instagram_user_id)%20%7B%20media.after(0%2C%20$count)%20%7B%0A%20%20count%2C%0A%20%20nodes%20%7B%0A%20%20%20%20caption%2C%0A%20%20%20%20code%2C%0A%20%20%20%20comments%20%7B%0A%20%20%20%20%20%20count%0A%20%20%20%20%7D%2C%0A%20%20%20%20date%2C%0A%20%20%20%20dimensions%20%7B%0A%20%20%20%20%20%20height%2C%0A%20%20%20%20%20%20width%0A%20%20%20%20%7D%2C%0A%20%20%20%20display_src%2C%0A%20%20%20%20id%2C%0A%20%20%20%20is_video%2C%0A%20%20%20%20likes%20%7B%0A%20%20%20%20%20%20count%0A%20%20%20%20%7D%2C%0A%20%20%20%20owner%20%7B%0A%20%20%20%20%20%20id%2C%0A%20%20%20%20%20%20username%2C%0A%20%20%20%20%20%20full_name%2C%0A%20%20%20%20%20%20profile_pic_url%0A%20%20%20%20%7D%2C%0A%20%20%20%20thumbnail_src%2C%0A%20%20%20%20video_views%0A%20%20%7D%2C%0A%20%20page_info%0A%7D%0A%20%7D" -k
我稍后会用 PHP 改进这个答案,我也需要用 PHP 来做这个。
【讨论】:
【参考方案5】:找到这篇中篇文章:- https://medium.com/@bkwebster/how-to-get-instagram-api-access-token-and-fix-your-broken-feed-c8ad470e3f02
<?php
$user_id=xxxxxx;//User ID is the first string of numbers before the first dot (.)
$count=2;
$width=100;
$height=100;
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=xxxxxx.83c3b89.257e2fd9c2bd40c181a2a4fb9576628c&count='.$count;
// Also Perhaps you should cache the results as the instagram API is slow
$cache = './'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60)
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache));
else
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
foreach ($jsonData->data as $key=>$value)
?>
<ul class="w3_footer_grid_list1">
<li><label class="fa fa-instagram" aria-hidden="true"></label><a target="_blank" href="<?php echo $value->link;?>"><i><?php echo $value->caption->text; ?> </i></a><?php ?>
</li>
<a target="_blank" href="<?php echo $value->link;?>">
<img src="<?php echo $value->images->low_resolution->url;?>" />
</a>
</ul>
<?php
?>
【讨论】:
【参考方案6】:此函数在您的 App 类中,但可以是常规函数,并且无论如何它都可以工作。
<?php
public function instagram()
$user = 'your user here';
// you can get your token from here: https://instagram.pixelunion.net/
$access_token = 'your access token here';
$photo_count = 6;// you can choose the amount. 20 is the max per query
$json_link = "https://api.instagram.com/v1/users/self/media/recent/?";
$json_link .="access_token=$access_token&count=$photo_count";
$json = file_get_contents($json_link);
return json_decode($json);
可以使用此工具交互式导航结果:http://jsonviewer.stack.hu/
就我而言,我使用的是刀片模板引擎 (https://laravel.com/docs/5.8/blade)
所以模板将是
@foreach($instagram->data as $gram)
<img src="$gram->images->thumbnail->url">
@endforeach
就是这样!
【讨论】:
【参考方案7】:答案应该更新,因为现在唯一可能的方法是通过 Instagram Facebook API --> https://developers.facebook.com/docs/instagram/oembed
【讨论】:
【参考方案8】:试试这个原始形式的爬虫类型。
function feed_instagram($url = "https://www.instagram.com/titaniumheart_")
//$url ie https://www.instagram.com/titaniumheart_
$dom = new DOMDocument();
@$dom->loadHTMLFile($url);
$f=$dom->saveHTML(); //load the url (crawl)
$key="";
$swquote=0;
echo "<div>";
for ($x=0;$x<strlen($f);$x++)
$c=substr($f,$x,1);
//echo $c."-";
if ($c==chr(34))
if($swquote==0)
$swquote=1; //to start get chars
else
$swquote=0;
//echo $key;
if($key=="code")
//get the number of comments
$m=substr($f,$x+4,100);
$code= substr($m,0,strpos($m,chr(34)));
echo "code is ".$code;
echo "<br>";
if($key=="comments")
//get the number of comments
$m=substr($f,$x+12,20);
$comments= substr($m,0,strpos($m,""));
echo "number of comments is ".$comments;
echo "<br>";
if($key=="caption")
//get the number of comments
$m=substr($f,$x+4,200);
$caption= substr($m,0,strpos($m,chr(34)));
echo "caption is ".$caption;
echo "<br>";
if($key=="likes")
//get the number of comments
$m=substr($f,$x+12,20);
$likes= substr($m,0,strpos($m,""));
echo "number of likes is ".$likes;
echo "<br>";
if($key=="thumbnail_src")
//get the number of comments
$m=substr($f,$x+4,200);
$src= substr($m,0,strpos($m,"?"));
echo "<br>image source is ".$src;
echo "<br>";
echo "<a href=\"https://www.instagram.com/p/".$code."/\">";
echo "<img src=\"".$src."\">";
echo "</a><br>";
$key="";
else
if($swquote==1)
$key.=$c;
echo "</div>";
用法:https://www.instagram.com/titaniumheart_");?>
注意:您必须在 php.ini 上启用扩展“php_openssl”。
【讨论】:
不使用用户要求的 Instagram API,这是加载前端 HTML 并遍历 DOM。以上是关于如何获取用户的 Instagram 提要的主要内容,如果未能解决你的问题,请参考以下文章
如何在不登录的情况下从 Instagram 获取 oauth 2 访问令牌(隐式流程)?
使用 facebook graph api 获取 instagram 自己/自我提要