I have a cron job that pulls data from Facebook very frequently all day long.
Many times per day, I get an error like this one:
FacebookAdsExceptionException: Connection timed out after 10001 milliseconds in /vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Adapter/CurlAdapter.php:196
Stack trace:
#0 /vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Client.php(204): FacebookAdsHttpAdapterCurlAdapter->sendRequest(Object(FacebookAdsHttpRequest))
#1 /vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Request.php(282): FacebookAdsHttpClient->sendRequest(Object(FacebookAdsHttpRequest))
#2 /vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(151): FacebookAdsHttpRequest->execute()
#3 /vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(193): FacebookAdsApi->executeRequest(Object(FacebookAdsHttpRequest))
#4 /vendor/facebook/php-ads-sdk/src/FacebookAds/ApiRequest.php(183): FacebookAdsApi->call('/act_1010015700...', 'GET', Array, Array)
#5 /app/Helpers/FacebookHelper.php(173): FacebookAdsApiRequest->execute()
#6 /app/Helpers/FacebookHelper.php(271): AppHelpersFacebookHelper->getInsights('AdAccount', 'act_10100157003...', 'ad', '2017-06-23', '2017-06-23', Array)
#7 /app/Helpers/FacebookHelper.php(360): AppHelpersFacebookHelper->getReport('AdAccount', 'act_10100157003...', 'ad', '2017-06-23', '2017-06-23', Array)
#8 /app/Console/Commands/CheckWhetherFacebookAdsStartedRunning.php(50): AppHelpersFacebookHelper->getDashAds('2017-06-23', '2017-06-23')
#9 [internal function]: AppConsoleCommandsCheckWhetherFacebookAdsStartedRunning->handle()
#10 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#11 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#12 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationApplication), Array, Object(Closure))
#13 /vendor/laravel/framework/src/Illuminate/Container/Container.php(539): IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), Array, Array, NULL)
#14 /vendor/laravel/framework/src/Illuminate/Console/Command.php(182): IlluminateContainerContainer->call(Array)
#15 /vendor/symfony/console/Command/Command.php(264): IlluminateConsoleCommand->execute(Object(SymfonyComponentConsoleInputArgvInput), Object(IlluminateConsoleOutputStyle))
#16 /vendor/laravel/framework/src/Illuminate/Console/Command.php(167): SymfonyComponentConsoleCommandCommand->run(Object(SymfonyComponentConsoleInputArgvInput), Object(IlluminateConsoleOutputStyle))
#17 /vendor/symfony/console/Application.php(869): IlluminateConsoleCommand->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#18 /vendor/symfony/console/Application.php(223): SymfonyComponentConsoleApplication->doRunCommand(Object(AppConsoleCommandsCheckWhetherFacebookAdsStartedRunning), Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#19 /vendor/symfony/console/Application.php(130): SymfonyComponentConsoleApplication->doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#20 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): SymfonyComponentConsoleApplication->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#21 /artisan(35): IlluminateFoundationConsoleKernel->handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#22 {main}
Level
----------------
ERROR
I see here in the Facebook API that CURLOPT_CONNECTTIMEOUT
is set to 10 seconds.
I’d like to try adjusting the timeout to 20 seconds.
I’ve been unable to figure out how to change that value without editing the source code in that library (which I definitely don’t want to do because then it will be overwritten on the next upgrade).
I’m calling $ad->getInsights($fields, $params, true);
(code) and then executing that request.
How can I override the Curl options that Facebook uses as a default?
2
Answers
In the file:
/facebook-php-ads-sdk/src/FacebookAds/Http/Adapter/CurlAdapter.php
I changed:
CURLOPT_CONNECTTIMEOUT => 10
to
CURLOPT_CONNECTTIMEOUT => 20
It works every time so far…
Had the same issue still in 2021 and was looking for an answer.
Looking through the issue from @Ryan comment and check the current state of the code.
So now in 2021 there is an "easy" way todo this: