0

我想將日期參數傳遞到PHP SDK批量請求方法。將日期參數傳遞給Facebook批量請求

是否有可能在日期PARAMS如通過:

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

我的批量要求基本上是:

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; 
    } 

回答

0

閱讀源代碼後,我發現您可以通過PARAMS這樣通過日期:(傳遞到請求處理程序時,它就會url_encoded)

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); 
    } 
0

根據

我會假設你可以添加一個additionial paramenter像這樣每個請求:

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

在PHP中,這將意味着你的必須json_encode$params陣列,並將其添加到每個請求。

$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));