guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP Fatal Error when retrieving certain pages.

triggerx opened this issue · comments

Guzzle version(s) affected: 7.8.0
PHP version: 8.1.17
cURL version: 7.68.0

Description
When trying to retrieve an Instagram profile page, it returns a "429 Too Many Requests" response that causes a PHP Fatal error, as shown below. I realize scraping Instagram is not recommended, but using here to demonstrate the issue I found.

mod_fcgid: stderr: PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://www.instagram.com/facebook/ resulted in a 429 Too Many Requests response in /home/user/.php/composer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113

How to reproduce
Attempt to get an Instagram Profile, such as https://www.instagram.com/facebook/.

Full PHP code on the page that calls Guzzle:
$url1 = 'https://www.instagram.com/facebook/';

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $url1);

echo $response->getStatusCode(); // 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'

Full Stack Trace
mod_fcgid: stderr: PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://www.instagram.com/facebook/ resulted in a 429 Too Many Requests response in /home/user/.php/composer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
mod_fcgid: stderr: Stack trace:
mod_fcgid: stderr: #0 /home/user/.php/composer/vendor/guzzlehttp/guzzle/src/Middleware.php(72): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL)
mod_fcgid: stderr: #1 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(209): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
mod_fcgid: stderr: #2 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(158): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL)
mod_fcgid: stderr: #3 /home/user/.php/composer/vendor/guzzlehttp/promises/src/TaskQueue.php(52): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
mod_fcgid: stderr: #4 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(251): GuzzleHttp\Promise\TaskQueue->run(true)
mod_fcgid: stderr: #5 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(227): GuzzleHttp\Promise\Promise->invokeWaitFn()
mod_fcgid: stderr: #6 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(272): GuzzleHttp\Promise\Promise->waitIfPending()
mod_fcgid: stderr: #7 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(229): GuzzleHttp\Promise\Promise->invokeWaitList()
mod_fcgid: stderr: #8 /home/user/.php/composer/vendor/guzzlehttp/promises/src/Promise.php(69): GuzzleHttp\Promise\Promise->waitIfPending()
mod_fcgid: stderr: #9 /home/user/.php/composer/vendor/guzzlehttp/guzzle/src/Client.php(189): GuzzleHttp\Promise\Promise->wait()
mod_fcgid: stderr: #10 /home/user/mysitename.com/guzzletest.php(8): GuzzleHttp\Client->request('GET', 'https://www.ins...')
mod_fcgid: stderr: #11 {main}
mod_fcgid: stderr: thrown in /home/user/.php/composer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

Guzzle is behaving correctly here. I don't know what the purpose of the issue was?

Ah, ok, thanks. I'll check the documentation to see what all responses will cause Guzzle to throw a fatal PHP error and see how to work around them. Thanks!

The fatal error is occurring simply because there is an uncaught exception, as per usual PHP behaviour.

Thank you Graham... I did indeed find what I was looking for in the documentation (http_errors => false). I know I know... RTFM.