ravelinodecastro/instagram-php-scraper

Instagram PHP Scraper. 获取账户信息、照片和视频,无需任何授权


README

这个库基于 postaddictmerestyler 版本。区别在于我的版本可以与任何代理API关联。我决定构建这个版本,因为restyler版本在返回这样的消息:“您的当前计划BASIC的请求每月配额已用尽”并且它只能与一个代理API一起工作,而postaddictme版本不允许我们直接使用代理API。

依赖关系

只有以下方法可以使用代理API,因为它们不需要身份验证

  • getAccount()
  • getAccountById()
  • getMedias()
  • getMediasByTag()
  • getMediaByUrl()
  • getMediaByCode()

使用restyler的Rapid Api示例(点击此处订阅其Proxy API

$instagram = new \InstagramScraper\Instagram();
$instagram->getProxyApiUrl('https://instagram40.p.rapidapi.com/proxy?url={instagram_url}'); // do not remove {instagram_url}, it is later replaced by the instagram endpoints
$instagram->setHeaders(['x-rapidapi-key'=>'YOUR-RAPID-API-KEY']); // this is where you add your api key
$nonPrivateAccountMedias = $instagram->getMedias('kevin');
echo $nonPrivateAccountMedias[0]->getLink();

使用scraperapi示例(点击此处订阅其Proxy API

$instagram = new \InstagramScraper\Instagram();
$instagram->getProxyApiUrl('http://api.scraperapi.com?api_key=YOUR-SCRAPPER-API-KEY&url={instagram_url}'); // do not remove {instagram_url}, it is later replaced by the instagram endpoints
$nonPrivateAccountMedias = $instagram->getMedias('kevin');
echo $nonPrivateAccountMedias[0]->getLink();

此库中还可以使用的其他方法是

  • getProxyApiUrl('string') : 用于检查和确认您的Proxy API url是否已设置
  • clearProxyApiUrl() : 如果您不再想使用代理API进行请求,则用于清除之前设置的url
  • setHeaders($array): 用于设置头部,如果您想在代理API请求中添加它们

从这里,所有其他部分都与postaddictme库中的相同

代码示例

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password');
$instagram->login();
$account = $instagram->getAccountById(3);
echo $account->getUsername();

某些方法不需要身份验证

$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());
$nonPrivateAccountMedias = $instagram->getMedias('kevin');
echo $nonPrivateAccountMedias[0]->getLink();

如果您使用身份验证,建议缓存用户会话。在这种情况下,您无需每次程序运行时都运行 $instagram->login() 方法

use Phpfastcache\Helper\Psr16Adapter;

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login(); // will use cached session if you want to force login $instagram->login(true)
$instagram->saveSession();  //DO NOT forget this in order to save the session, otherwise have no sense
$account = $instagram->getAccountById(3);
echo $account->getUsername();

使用代理进行请求

// https://docs.guzzlephp.org/en/stable/request-options.html#proxy
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client(['proxy' => 'tcp://:8125']));
// Request with proxy
$account = $instagram->getAccount('kevin');
\InstagramScraper\Instagram::setHttpClient(new \GuzzleHttp\Client());
// Request without proxy
$account = $instagram->getAccount('kevin');

安装

使用composer

composer.phar require ravelinodecastro/instagram-php-scraper

composer require ravelinodecastro/instagram-php-scraper

如果您没有composer

您可以从这里下载。

示例

请参阅示例 这里

其他

Java库: https://github.com/postaddictme/instagram-java-scraper