0

I want to pass in date parameters into the PHP SDK batch request method.

Is it possible to pass in date params such as:

$params = array(
  'time_range' => array(
    'since' => (new \DateTime("-1 week"))->format('Y-m-d'),
    'until' => (new \DateTime())->format('Y-m-d'),
  ),
);

My batch request is essentially:

foreach( $campaign_ids as $campaign_id ) {  

    $batch[] = $fb->request('GET/', env("FB_APP_VERSION") . '/' . $campaign_id . '/insights?fields=impressions,unique_clicks,reach');    

}

    try {
      $responses = $fb->sendBatchRequest($batch);
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
    }
Stretch0
  • 8,362
  • 13
  • 71
  • 133

2 Answers2

0

According to

I'd assume you can add an additionial paramenter to each request like this:

Format: time_range={'since':'2015-01-01','until':'2015-01-20'}

In PHP, this would mean you's have to json_encode the $params array and add it to each request.

$time_range = array(
    'since' => (new \DateTime("-1 week"))->format('Y-m-d'),
    'until' => (new \DateTime())->format('Y-m-d'),
)

$fb->request('GET/', env("FB_APP_VERSION") . '/' . $campaign_id . '/insights?fields=impressions,unique_clicks,reach&time_range=' . json_encode($time_range));
Tobi
  • 31,405
  • 8
  • 58
  • 90
0

After reading the source code, I found you can pass the date through in params as such: (it gets url_encoded when passed to the request handler)

foreach( $campaigns as $campaign ) {  

        $params = array(
          'time_range' => array(
            'since' => (new \DateTime($campaign->data_date . ' -1 day'))->format('Y-m-d'),
            'until' => (new \DateTime($campaign->data_date))->format('Y-m-d')
          ),
        );

        $batch[] = $fb->request('GET', '/' . $campaign->ad_campaign_id . '/insights?fields=impressions,unique_clicks,reach', $params);
    }
Stretch0
  • 8,362
  • 13
  • 71
  • 133