From b265382663e639015b650875685ddc6c8d541b1e Mon Sep 17 00:00:00 2001 From: Sergei Gulin Date: Thu, 22 Oct 2020 19:18:10 +0300 Subject: [PATCH] Psr18 (#796) * add psr-18 http client * upd README.md * upd phpunit * guzzlehttp/psr7 --- README.md | 20 ++--- composer.json | 12 +-- examples/addAndDeleteComment.php | 2 +- examples/getAccountById.php | 2 +- examples/getAccountByUsername.php | 2 +- examples/getAccountFollowers.php | 2 +- examples/getAccountFollowings.php | 2 +- examples/getAccountMediasByUsername.php | 4 +- examples/getCurrentTopMediasByLocationId.php | 2 +- examples/getCurrentTopMediasByTagName.php | 2 +- examples/getFeed.php | 2 +- examples/getHighlights.php | 2 +- examples/getLocationById.php | 2 +- examples/getMediaByCode.php | 4 +- examples/getMediaById.php | 4 +- examples/getMediaByUrl.php | 4 +- examples/getMediaComments.php | 2 +- examples/getMediasByLocationId.php | 2 +- examples/getMediasByTag.php | 2 +- examples/getPaginateMediasByTag.php | 2 +- examples/getPaginateMediasByUsername.php | 2 +- examples/getSidecarMediaByUrl.php | 4 +- examples/getStories.php | 2 +- examples/getThreads.php | 2 +- examples/likeAndUnlikeMedia.php | 2 +- examples/paginateAccountMediaByUsername.php | 2 +- examples/searchAccountsByUsername.php | 2 +- examples/setCustomCookies.php | 2 +- .../loginWithEmailAutoCponfirm.php | 2 +- src/InstagramScraper/Endpoints.php | 2 +- src/InstagramScraper/Http/Request.php | 81 +++++++++++++++++++ src/InstagramScraper/Http/Response.php | 38 +++++++++ src/InstagramScraper/Instagram.php | 74 ++++++----------- tests/InstagramTest.php | 5 +- 34 files changed, 193 insertions(+), 103 deletions(-) create mode 100644 src/InstagramScraper/Http/Request.php create mode 100644 src/InstagramScraper/Http/Response.php diff --git a/README.md b/README.md index a3ab5604..8c5f954c 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,14 @@ 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(); @@ -16,7 +17,7 @@ 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(); ``` @@ -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); @@ -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'); ``` diff --git a/composer.json b/composer.json index 9ac61f04..a495ec59 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/examples/addAndDeleteComment.php b/examples/addAndDeleteComment.php index ca1aae97..4945e539 100644 --- a/examples/addAndDeleteComment.php +++ b/examples/addAndDeleteComment.php @@ -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 { diff --git a/examples/getAccountById.php b/examples/getAccountById.php index a237c152..c5d628db 100644 --- a/examples/getAccountById.php +++ b/examples/getAccountById.php @@ -1,7 +1,7 @@ getAccountById('3'); +$account = (new \InstagramScraper\Instagram(new \GuzzleHttp\Client()))->getAccountById('3'); // Available fields echo "Account info:\n"; diff --git a/examples/getAccountByUsername.php b/examples/getAccountByUsername.php index 07633363..1587499b 100644 --- a/examples/getAccountByUsername.php +++ b/examples/getAccountByUsername.php @@ -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: diff --git a/examples/getAccountFollowers.php b/examples/getAccountFollowers.php index 74e6ce24..16cbc315 100644 --- a/examples/getAccountFollowers.php +++ b/examples/getAccountFollowers.php @@ -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 diff --git a/examples/getAccountFollowings.php b/examples/getAccountFollowings.php index 27e29290..6d4aea80 100644 --- a/examples/getAccountFollowings.php +++ b/examples/getAccountFollowings.php @@ -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 diff --git a/examples/getAccountMediasByUsername.php b/examples/getAccountMediasByUsername.php index b55681df..142fa75c 100644 --- a/examples/getAccountMediasByUsername.php +++ b/examples/getAccountMediasByUsername.php @@ -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 @@ -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); diff --git a/examples/getCurrentTopMediasByLocationId.php b/examples/getCurrentTopMediasByLocationId.php index 5523cd1a..50673209 100644 --- a/examples/getCurrentTopMediasByLocationId.php +++ b/examples/getCurrentTopMediasByLocationId.php @@ -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'); diff --git a/examples/getCurrentTopMediasByTagName.php b/examples/getCurrentTopMediasByTagName.php index 1f38eb51..5533d021 100644 --- a/examples/getCurrentTopMediasByTagName.php +++ b/examples/getCurrentTopMediasByTagName.php @@ -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'); diff --git a/examples/getFeed.php b/examples/getFeed.php index 75249a6f..f9532797 100644 --- a/examples/getFeed.php +++ b/examples/getFeed.php @@ -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(); diff --git a/examples/getHighlights.php b/examples/getHighlights.php index 79286e0a..a4858f14 100644 --- a/examples/getHighlights.php +++ b/examples/getHighlights.php @@ -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(); diff --git a/examples/getLocationById.php b/examples/getLocationById.php index 35be50b1..e1b9ee3a 100644 --- a/examples/getLocationById.php +++ b/examples/getLocationById.php @@ -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 diff --git a/examples/getMediaByCode.php b/examples/getMediaByCode.php index e0d8c5ae..7c032562 100644 --- a/examples/getMediaByCode.php +++ b/examples/getMediaByCode.php @@ -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'); diff --git a/examples/getMediaById.php b/examples/getMediaById.php index 5e47c8d8..e21bd41f 100644 --- a/examples/getMediaById.php +++ b/examples/getMediaById.php @@ -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'); diff --git a/examples/getMediaByUrl.php b/examples/getMediaByUrl.php index b16839f1..24b812cd 100644 --- a/examples/getMediaByUrl.php +++ b/examples/getMediaByUrl.php @@ -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'); diff --git a/examples/getMediaComments.php b/examples/getMediaComments.php index d841a9b1..dc3385ed 100644 --- a/examples/getMediaComments.php +++ b/examples/getMediaComments.php @@ -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 diff --git a/examples/getMediasByLocationId.php b/examples/getMediasByLocationId.php index 12372207..0ca7dfeb 100644 --- a/examples/getMediasByLocationId.php +++ b/examples/getMediasByLocationId.php @@ -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); diff --git a/examples/getMediasByTag.php b/examples/getMediasByTag.php index 43e2025c..bd3e0e34 100644 --- a/examples/getMediasByTag.php +++ b/examples/getMediasByTag.php @@ -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); diff --git a/examples/getPaginateMediasByTag.php b/examples/getPaginateMediasByTag.php index 43fc02ba..b8038973 100644 --- a/examples/getPaginateMediasByTag.php +++ b/examples/getPaginateMediasByTag.php @@ -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'); diff --git a/examples/getPaginateMediasByUsername.php b/examples/getPaginateMediasByUsername.php index 820b6895..3a0d55bd 100644 --- a/examples/getPaginateMediasByUsername.php +++ b/examples/getPaginateMediasByUsername.php @@ -1,7 +1,7 @@ getPaginateMedias('kevin'); foreach ($response['medias'] as $media) { diff --git a/examples/getSidecarMediaByUrl.php b/examples/getSidecarMediaByUrl.php index b8e718da..4bb90ca0 100644 --- a/examples/getSidecarMediaByUrl.php +++ b/examples/getSidecarMediaByUrl.php @@ -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'); diff --git a/examples/getStories.php b/examples/getStories.php index c602ccf2..bcc8b392 100644 --- a/examples/getStories.php +++ b/examples/getStories.php @@ -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(); diff --git a/examples/getThreads.php b/examples/getThreads.php index 84653fa7..fd72cfdc 100644 --- a/examples/getThreads.php +++ b/examples/getThreads.php @@ -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(); diff --git a/examples/likeAndUnlikeMedia.php b/examples/likeAndUnlikeMedia.php index 55b3d165..99cfc54f 100644 --- a/examples/likeAndUnlikeMedia.php +++ b/examples/likeAndUnlikeMedia.php @@ -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 { diff --git a/examples/paginateAccountMediaByUsername.php b/examples/paginateAccountMediaByUsername.php index aa862cd7..2d155c57 100644 --- a/examples/paginateAccountMediaByUsername.php +++ b/examples/paginateAccountMediaByUsername.php @@ -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'); diff --git a/examples/searchAccountsByUsername.php b/examples/searchAccountsByUsername.php index 6b25f512..72b8fd39 100644 --- a/examples/searchAccountsByUsername.php +++ b/examples/searchAccountsByUsername.php @@ -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(); diff --git a/examples/setCustomCookies.php b/examples/setCustomCookies.php index 2ee21186..b2de2f5a 100644 --- a/examples/setCustomCookies.php +++ b/examples/setCustomCookies.php @@ -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(); diff --git a/examples/twoStepAutoVerification/loginWithEmailAutoCponfirm.php b/examples/twoStepAutoVerification/loginWithEmailAutoCponfirm.php index b4c5def1..dafb3510 100644 --- a/examples/twoStepAutoVerification/loginWithEmailAutoCponfirm.php +++ b/examples/twoStepAutoVerification/loginWithEmailAutoCponfirm.php @@ -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( 'user@mail.ru', diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 08c14dbd..a76196a6 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -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'; diff --git a/src/InstagramScraper/Http/Request.php b/src/InstagramScraper/Http/Request.php new file mode 100644 index 00000000..d12c41dc --- /dev/null +++ b/src/InstagramScraper/Http/Request.php @@ -0,0 +1,81 @@ +sendRequest($request)); + } + + /** + * Send a GET request to a URL + * + * @param string $url URL to send the GET request to + * @param array $headers additional headers to send + * @param array|null $parameters parameters to send in the querystring + * @return Response + * @throws ClientExceptionInterface + */ + public static function get(string $url, array $headers = [], array $parameters = null) + { + $uri = new Uri($url); + if ($parameters !== null) { + $uri = $uri->withQuery(http_build_query($parameters)); + } + return self::send('GET', $uri, $headers); + } + + /** + * @param string $url + * @param array $headers + * @param array|null $body + * @return Response + * @throws ClientExceptionInterface + */ + public static function post(string $url, array $headers = [], array $body = null) + { + return self::send('POST', $url, $headers, $body); + } +} \ No newline at end of file diff --git a/src/InstagramScraper/Http/Response.php b/src/InstagramScraper/Http/Response.php new file mode 100644 index 00000000..7e63e2ec --- /dev/null +++ b/src/InstagramScraper/Http/Response.php @@ -0,0 +1,38 @@ +code = $response->getStatusCode(); + $this->headers = $response->getHeaders(); + $raw_body = $response->getBody()->getContents(); + $this->raw_body = $raw_body; + $this->body = $raw_body; + + if (function_exists('json_decode')) { + $json = json_decode($raw_body, false, 512, JSON_BIGINT_AS_STRING); + + if (json_last_error() === JSON_ERROR_NONE) { + $this->body = $json; + } + } + } +} \ No newline at end of file diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index c8f4a363..5b2e7979 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -9,6 +9,7 @@ use InstagramScraper\Exception\InstagramException; use InstagramScraper\Exception\InstagramNotFoundException; use InstagramScraper\Exception\InstagramAgeRestrictedException; +use InstagramScraper\Http\Response; use InstagramScraper\Model\Account; use InstagramScraper\Model\Activity; use InstagramScraper\Model\Comment; @@ -23,10 +24,10 @@ use InstagramScraper\TwoStepVerification\ConsoleVerification; use InstagramScraper\TwoStepVerification\TwoStepVerificationInterface; use InvalidArgumentException; +use InstagramScraper\Http\Request; +use Psr\Http\Client\ClientInterface; use Psr\SimpleCache\CacheInterface; use stdClass; -use Unirest\Request; -use Unirest\Response; class Instagram { @@ -57,16 +58,34 @@ class Instagram private $customCookies = null; /** + * Instagram constructor. + * @param ClientInterface $client + */ + public function __construct(ClientInterface $client) + { + Request::setHttpClient($client); + } + + /** + * @param ClientInterface $httpClient + */ + public static function setHttpClient(ClientInterface $httpClient): void + { + Request::setHttpClient($httpClient); + } + + /** + * @param ClientInterface $client * @param string $username * @param string $password * @param CacheInterface $cache * * @return Instagram */ - public static function withCredentials($username, $password, $cache) + public static function withCredentials(ClientInterface $client, $username, $password, $cache) { static::$instanceCache = $cache; - $instance = new self(); + $instance = new self($client); $instance->sessionUsername = $username; $instance->sessionPassword = $password; return $instance; @@ -138,53 +157,6 @@ public static function setAccountMediasRequestCount($count) Endpoints::setAccountMediasRequestCount($count); } - /** - * Set custom curl opts - */ - public static function curlOpts($opts) - { - Request::curlOpts($opts); - } - - /** - * @param array $config - */ - public static function setProxy(array $config) - { - $defaultConfig = [ - 'port' => false, - 'tunnel' => false, - 'address' => false, - 'type' => CURLPROXY_HTTP, - 'timeout' => false, - 'auth' => [ - 'user' => '', - 'pass' => '', - 'method' => CURLAUTH_BASIC - ], - ]; - - $config = array_replace($defaultConfig, $config); - - Request::proxy($config['address'], $config['port'], $config['type'], $config['tunnel']); - - if (isset($config['auth'])) { - Request::proxyAuth($config['auth']['user'], $config['auth']['pass'], $config['auth']['method']); - } - - if (isset($config['timeout'])) { - Request::timeout((int)$config['timeout']); - } - } - - /** - * Disable proxy for all requests - */ - public static function disableProxy() - { - Request::proxy(''); - } - /** * @param string $username * diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index dcb98dbd..2f1cc953 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -2,6 +2,7 @@ namespace InstagramScraper\Tests; +use GuzzleHttp\Client; use InstagramScraper\Instagram; use InstagramScraper\Model\Media; use Phpfastcache\Config\ConfigurationOption; @@ -24,7 +25,7 @@ public static function setUpBeforeClass() ]); $instanceCache = new Psr16Adapter($defaultDriver, $options); - self::$instagram = Instagram::withCredentials($_ENV['LOGIN'], $_ENV['PASSWORD'], $instanceCache); + self::$instagram = Instagram::withCredentials(new Client(), $_ENV['LOGIN'], $_ENV['PASSWORD'], $instanceCache); if (isset($_ENV['USER_AGENT'])) { self::$instagram->setUserAgent($_ENV['USER_AGENT']); @@ -150,7 +151,7 @@ public function testGetUsernameById() */ public function testGetMediasByUserId() { - $instagram = new Instagram(); + $instagram = new Instagram(new Client()); $nonPrivateAccountMedias = $instagram->getMediasByUserId(3); $this->assertEquals(12, count($nonPrivateAccountMedias)); }