ConRockets ConRockets - 1 year ago 123
PHP Question

Trouble understanding Facebook Uncaught Exception

I'm super new to Facebook PHP SDK. I am having trouble understanding the stack trace below; where should I look for the error?

The Facebook Scheduled posts are successfully processing, however, this error causes a bad user experience, but, everything else in my program is working fine. It looks like it may be trying to process something that isn't there.

The last line of code being executed (I know because the scheduled posts show up in my Facebook Test Account) is:

$responses = $fb->sendBatchRequest(($batch), $this->session->userdata('fb_access_token'));

$batch is defined as:

foreach($esdarr as $key => $event_start_datetime){$sched_time = strtotime($event_start_datetime)- 18000;
$event_end_datetime = $eedarr[$key];
$requestScheduleEvent = $fb->post('/'.$band_page_id.'/feed', ['message' => $band_name.' is playing today at '.$venue_name.' '.date('(l), g:ia', strtotime($event_start_datetime)), 'published'=>false, 'scheduled_publish_time'=>$sched_time], $pageToken);

This is all within a CI Controller (event.php) with public function add(). Then I get this error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Argument for add() must be of type array or FacebookRequest.' in /home/my_user/public_html/development/application/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php:85 Stack trace:
#0 /home/my_user/public_html/development/application/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php(78): Facebook\FacebookBatchRequest->add(Object(Facebook\FacebookResponse), 0)
#1 /home/my_user/public_html/development/application/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php(61): Facebook\FacebookBatchRequest->add(Array)
#2 /home/my_user/public_html/development/application/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(492): Facebook\FacebookBatchRequest->__construct(Object(Facebook\FacebookApp), Array, 'EAAYzEGW0ZAsoBA...', 'v2.7')
#3 /home/my_user/public_html/development/application/controllers/user/event.php(777): Facebook\Facebook->sendBatchRequest(Array, 'EAAYzEGW0ZAsoBA...')
#4 [internal function]: Event->add in /home/my_user/public_html/development/application/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php on line 85

If I change line #85 of FacebookBatchRequest.php as follows (that is; redirect the page to the correct URL within the CI structure), everything seems to work fine:

public function add($request, $name = null)
if (is_array($request)) {
foreach ($request as $key => $req) {
$this->add($req, $key);

return $this;

if (!$request instanceof FacebookRequest) {
throw new \InvalidArgumentException('Argument for add() must be of type array or FacebookRequest.');

$requestToAdd = [
'name' => $name,
'request' => $request,

Of course, this is a tenuous workaround at best. So, I'd like to know why the error is being thrown in the first place.

Answer Source

Okay, this ended up being an erroneous use of the batch process on my part.

$requestScheduleEvent = $fb->post('/'.$band_page_id.'/feed', ['message' => $band_name.' is playing today at '.$venue_name.' '.date('(l), g:ia', strtotime($event_start_datetime)), 'published'=>false, 'scheduled_publish_time'=>$sched_time], $pageToken);

Needed to use the request method:

$batch[$key] = $fb->request('POST', '/'.$band_page_id.'/feed', ['message' => $band_name.' is playing today at '.$venue_name.' '.date('(l), g:ia', strtotime($event_start_datetime)), 'published'=>false, 'scheduled_publish_time'=>$sched_time], $pageToken);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download