I'm using Facebook Ads SDK and trying to add users to a custom audience list. As there is a 10000 data limit at a time, for future proof I am trying to add data in a loop.
However, when it hits the second time, it throws an error.
public function test($audienceId, $data)
Api::init($appId, $appSecret, $token);
$audience = new CustomAudience($audienceId);
$schema = [
CustomAudienceMultikeySchemaFields::EMAIL,
CustomAudienceMultikeySchemaFields::PHONE,
//...
];
foreach (array_chunk($data, 500) as $chunk) {
$audience->addUsersMultiKey($chunk, $schema, true);
}
}
// And run as
$class = new FbTestClass();
$class->test($audienceId, $data);
If the addUsersMultiKey()
runs 1 time, it works perfectly, however if it runs 2 times, it throws an error
FacebookAds\Http\Exception\AuthorizationException - An unknown error has occurred.
App:init()
part to __construct()
, but the issue persists.public function __construct() {
Api::init($appId, $appSecret, $token);
}
public function test($audienceId) {
$audience = new CustomAudience($audienceId);
$schema = [
CustomAudienceMultikeySchemaFields::EMAIL, //...
];
$audience->addUsersMultiKey($chunk, $schema, true);
}
// And run as:
$class = new FbTestClass();
foreach (array_chunk($data, 500) as $chunk) {
$class->test($audienceId, $chunk);
}
test()
function, and keeping App::init()
inside the test()
function, so App::init()
gets called multiple times. Still same issue.This answers says to do the requests individually but how is it possible to do that? But, didn't I try all possible scenarios?
The error seems to be with batched requests - The solution for us was to change the code to do the requests individually.
I believe it's about the runtime because when I refresh, it works alright; so I don't think it's throttling issue (I also tried to sleep(10)
, in fact it works on refresh).
How can I overcome this issue?
Maybe there is a way to destroy App
instance? Or is there a way to create a new App
instance to overwrite previous one?
I saw a batch request in the facebook docs but couldn't figure out how to use it with my use-case (custom audience & addUsersMultiKey()).
Stack Trace:
An unknown error has occurred. {"userId":1,"exception":"[object] (FacebookAds\\Http\\Exception\\AuthorizationException(code: 1): An unknown error has occurred. at /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php:165)
[stacktrace]
#0 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Client.php(215): FacebookAds\\Http\\Exception\\RequestException::create()
#1 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Request.php(286): FacebookAds\\Http\\Client->sendRequest()
#2 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(165): FacebookAds\\Http\\Request->execute()
#3 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(214): FacebookAds\\Api->executeRequest()
#4 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Object/CustomAudience.php(537): FacebookAds\\Api->call()
#5 is where I execute the code:
$audience->addUsersMultiKey($chunk, $schema, true);
Update: 4 I also tried same thing with graph-sdk
$fb = new Facebook([
'app_id' => env("FB_APP_ID"),
'app_secret' => env("FB_APP_SECRET"),
'default_graph_version' => 'v6.0',
'default_access_token' => env("FB_ACCESS_TOKEN")
]);
foreach ($chunks as $chunk) {
$payload = [
'schema' => $schema,
'data' => $chunk->toArray()
];
$requests[] = $fb->request("POST", "/$customAudienceId/users", ['payload' => $payload]);
}
$batchResponse = $fb->sendBatchRequest($requests);
Here, response array has both request. 1st one is successful, second one same issue.
$batchResponse contains:
responses: array:2 [▼
0 => Facebook\FacebookResponse ▼
body => // successful data returned...
1 => Facebook\FacebookResponse ▼
body => "{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1,"fbtrace_id":"A5...464cGc22gi28Kb"}}"
I found the bug, it was actually from my end.
When I was chunking, my indexes in chunks were preserving their indexes of actual array, and as it wasn't giving [0,1,2], it was causing the api call to fail.
array_values($chunk);
should fix that issue.
for Laravel's collection $chunk->values()->toArray();