Skip to content

Commit

Permalink
Psr18 (#796)
Browse files Browse the repository at this point in the history
* add psr-18 http client
* upd README.md
* upd phpunit
* guzzlehttp/psr7
  • Loading branch information
Sergei Gulin authored Oct 22, 2020
1 parent cd9e475 commit b265382
Show file tree
Hide file tree
Showing 34 changed files with 193 additions and 103 deletions.
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
This library is based on the Instagram web version. We develop it because nowadays it is hard to get an approved Instagram application. The purpose is to support every feature that the web desktop and mobile version support.

## Dependencies

- PHP >= 7.2
- [PSR-16](http://www.php-fig.org/psr/psr-16/)
- [PSR-18](http://www.php-fig.org/psr/psr-18/)


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

Some methods do not require authentication:
```php
$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());
$nonPrivateAccountMedias = $instagram->getMedias('kevin');
echo $nonPrivateAccountMedias[0]->getLink();
```
Expand All @@ -26,7 +27,7 @@ If you use authentication it is recommended to cache the user session. In this c
```php
use Phpfastcache\Helper\Psr16Adapter;

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$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);
Expand All @@ -36,16 +37,11 @@ echo $account->getUsername();
Using proxy for requests:

```php
$instagram = new \InstagramScraper\Instagram();
Instagram::setProxy([
'address' => '111.112.113.114',
'port' => '8080',
'tunnel' => true,
'timeout' => 30,
]);
// https://docs.guzzlephp.org/en/stable/request-options.html#proxy
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client(['proxy' => 'tcp://localhost:8125']));
// Request with proxy
$account = $instagram->getAccount('kevin');
Instagram::disableProxy();
\InstagramScraper\Instagram::setHttpClient(new \GuzzleHttp\Client());
// Request without proxy
$account = $instagram->getAccount('kevin');
```
Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
}
],
"require": {
"php": ">=5.4.0",
"mashape/unirest-php": "3.0.*",
"php": ">=7.2",
"ext-curl": "*",
"ext-json": "*",
"psr/simple-cache": "~1.0"
"psr/simple-cache": "~1.0",
"psr/http-client": "~1.0",
"guzzlehttp/psr7": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "5.5.*",
"phpfastcache/phpfastcache": "^7.1"
"phpunit/phpunit": "^7.0",
"phpfastcache/phpfastcache": "^7.1",
"guzzlehttp/guzzle": "^7.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion examples/addAndDeleteComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/getAccountById.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$account = (new \InstagramScraper\Instagram())->getAccountById('3');
$account = (new \InstagramScraper\Instagram(new \GuzzleHttp\Client()))->getAccountById('3');

// Available fields
echo "Account info:\n";
Expand Down
2 changes: 1 addition & 1 deletion examples/getAccountByUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// If account is public you can query Instagram without auth

$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());

// For getting information about account you don't need to auth:

Expand Down
2 changes: 1 addition & 1 deletion examples/getAccountFollowers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();
sleep(2); // Delay to mimic user

Expand Down
2 changes: 1 addition & 1 deletion examples/getAccountFollowings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();
sleep(2); // Delay to mimic user

Expand Down
4 changes: 2 additions & 2 deletions examples/getAccountMediasByUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// If account is public you can query Instagram without auth

$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());
$medias = $instagram->getMedias('kevin', 25);

// Let's look at $media
Expand All @@ -30,6 +30,6 @@


// If account private you should be subscribed and after auth it will be available
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();
$medias = $instagram->getMedias('private_account', 100);
2 changes: 1 addition & 1 deletion examples/getCurrentTopMediasByLocationId.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$medias = $instagram->getCurrentTopMediasByLocationId('1');
Expand Down
2 changes: 1 addition & 1 deletion examples/getCurrentTopMediasByTagName.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$medias = $instagram->getCurrentTopMediasByTagName('youneverknow');
Expand Down
2 changes: 1 addition & 1 deletion examples/getFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use InstagramScraper\Instagram;
use Phpfastcache\Helper\Psr16Adapter;

$instagram = Instagram::withCredentials('login', 'password', new Psr16Adapter('Files'));
$instagram = Instagram::withCredentials(new \GuzzleHttp\Client(), 'login', 'password', new Psr16Adapter('Files'));
$instagram->login();
$instagram->saveSession();

Expand Down
2 changes: 1 addition & 1 deletion examples/getHighlights.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$userId = $instagram->getAccount('instagram')->getId();
Expand Down
2 changes: 1 addition & 1 deletion examples/getLocationById.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

// Location id from facebook
Expand Down
4 changes: 2 additions & 2 deletions examples/getMediaByCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
require __DIR__ . '/../vendor/autoload.php';

// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());

// If account is private and you subscribed to it, first login
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$media = $instagram->getMediaByCode('BHaRdodBouH');
Expand Down
4 changes: 2 additions & 2 deletions examples/getMediaById.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
require __DIR__ . '/../vendor/autoload.php';

// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());

// If account is private and you subscribed to it, first login
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$media = $instagram->getMediaById('1270593720437182847');
Expand Down
4 changes: 2 additions & 2 deletions examples/getMediaByUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
require __DIR__ . '/../vendor/autoload.php';

// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());

// If account is private and you subscribed to it, first login
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BHaRdodBouH');
Expand Down
2 changes: 1 addition & 1 deletion examples/getMediaComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

// Get media comments by shortcode
Expand Down
2 changes: 1 addition & 1 deletion examples/getMediasByLocationId.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$medias = $instagram->getMediasByLocationId('1', 20);
Expand Down
2 changes: 1 addition & 1 deletion examples/getMediasByTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$medias = $instagram->getMediasByTag('youneverknow', 20);
Expand Down
2 changes: 1 addition & 1 deletion examples/getPaginateMediasByTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$result = $instagram->getPaginateMediasByTag('zara');
Expand Down
2 changes: 1 addition & 1 deletion examples/getPaginateMediasByUsername.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());
$response = $instagram->getPaginateMedias('kevin');

foreach ($response['medias'] as $media) {
Expand Down
4 changes: 2 additions & 2 deletions examples/getSidecarMediaByUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function printMediaInfo(\InstagramScraper\Model\Media $media, $padding = '') {
}

// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();
$instagram = new \InstagramScraper\Instagram(new \GuzzleHttp\Client());

// If account is private and you subscribed to it firstly login
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BQ0lhTeAYo5');
Expand Down
2 changes: 1 addition & 1 deletion examples/getStories.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$stories = $instagram->getStories();
Expand Down
2 changes: 1 addition & 1 deletion examples/getThreads.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();
$instagram->saveSession();

Expand Down
2 changes: 1 addition & 1 deletion examples/likeAndUnlikeMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/paginateAccountMediaByUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require __DIR__ . '/../vendor/autoload.php';

// getPaginateMedias() works with and without authentication
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();

$result = $instagram->getPaginateMedias('kevin');
Expand Down
2 changes: 1 addition & 1 deletion examples/searchAccountsByUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram->login();


Expand Down
2 changes: 1 addition & 1 deletion examples/setCustomCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ds_user_id" => "36*****872",
];

$instagram = \InstagramScraper\Instagram::withCredentials($this->instaUsername, $this->instaPassword, new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $this->instaUsername, $this->instaPassword, new Psr16Adapter('Files'));
$instagram->setUserAgent('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0');
$instagram->setCustomCookies($newCookie);
$instagram->login();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* EmailVerification requires https://packagist.org/packages/php-mail-client/client
*/

$instagram = \InstagramScraper\Instagram::withCredentials('user', 'password');
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'user', 'password', new \Phpfastcache\Helper\Psr16Adapter('Files'));

$emailVecification = new EmailVerification(
'[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Endpoints
{
const BASE_URL = 'https://www.instagram.com';
const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/';
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}';
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}/';
const MEDIA_LINK = 'https://www.instagram.com/p/{code}';
const ACCOUNT_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables={variables}';
const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1';
Expand Down
Loading

0 comments on commit b265382

Please sign in to comment.