问题
I am trying to get list of all cities of a country from Facebook.
I started from FB documentation of v2.9 here: Facebook Marketing API
And tried of getting list of cities as described using curl:
curl -G \
-d 'location_types=["city"]' \
-d 'type=adgeolocation' \
-d 'q=dub' \
-d 'access_token=<MY_TOKEN>' \
https://graph.facebook.com/v2.9/search
But it returns empty json. If i remove q=dub parameter it still returns empty data. But if i put country as a type of location, then it nicely returns country list. What am i doing wrong?
It would be also appriciated if you could point how to make it return cities for certain country. There is country_code parameter, but it also doesn't work.
回答1:
Little weird but I was trying this for last few hours and it turns out the search for city is not available when you are using access token that is of a Sandbox account.
I tried the same api endpoints with real facebook advert account's access token and it works.
as of how this works, here is a bit of code that might help future readers :
//Include Facebook SDK classes
//use FacebookAds\Api;
//use FacebookAds\ApiRequest;
//use FacebookAds\Object\TargetingSearch;
//use FacebookAds\Object\Search\TargetingSearchTypes;
function searcher() {
$fbutils = new FacebookUtils(16);
$account_id = $fbutils->getAccountID();
$accessToken = $fbutils->getAccessToken($account_id);
Api::init(fb_app_id, fb_app_secret, $accessToken);
$result = TargetingSearch::search(
TargetingSearchTypes::GEOLOCATION, null, 'dub', array(
'location_types' => array('city'),
));
$fb = new Facebook([
'app_id' => fb_app_id,
'app_secret' => fb_app_secret,
'default_graph_version' => 'v2.10',
'default_access_token' => $accessToken
]);
$fb = $result;
$objects = $result->getArrayCopy();
$objects_arr = array();
do {
foreach ($objects as $object) {
$objects_arr[] = $object->getData();
}
} while ($objects = $fb->next($objects));
echo "<pre>";
print_r($objects_arr);
exit;
}
Note : replace constants and variables with your data and this should work for searching cities and use pagination and print result as a pretty array.
来源:https://stackoverflow.com/questions/44970042/facebook-marketing-api-geographic-search