How do I write PHP curl code and use proper param formats to pull back Facebook Ads reach estimate -
facebook provides shell curl command example how pull reach estimate. so..
curl -g \ -d "currency=usd" \ -d "targeting_spec=___" \ -d "access_token=___" \ "https://graph.facebook.com/<api_version>/act_<ad_account_id>/reachestimate"
how format targeting_specs params correctly , write php curl extension?
a couple of things note here.
when converting shell curl command php, 1 might assume since targeting_spec have lot of data, best way facebook graph post data. facebook graph graph call not accept post, returns (invalid post error when try) fount out need use param string
$postdata = array( 'currency' => 'usd', 'access_token' => $this->_access_token, 'targeting_spec' => urlencode(json_encode($targetingarray)), );
targeting array contain targeting data such genders, age_min, age_max, zips etc, plus advanced demographics might have such behaviors, interests, income, net_worth, etc. these need formatted in json string. can creating php array matches json structure , using json_encode.
to see end result format targeting_spec, follow examples given in targeting specs docs. see url https://developers.facebook.com/docs/marketing-api/targeting-specs/v2.3
note: advanced demographics in targeting specs contain name values in json , since facebook requires string in case, need urlencode targeting_spec param json string shown above.
$ch = curl_init(); curl_setopt($ch, curlopt_url, "https://graph.facebook.com/v2.2/" . $this->_ad_account->id . "/reachestimate" . '?access_token=' . $postdata['access_token'] . '&' . 'targeting_spec=' . $postdata['targeting_spec'] . '&' . 'currency=' . 'usd' ); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, 2); curl_setopt($ch, curlopt_returntransfer, true); $result = curl_exec($ch); curl_close($ch);