FB Development - How to search facebook cities by their "key"

1.5k Views Asked by At

Situation:

I'm currently working on a project where a user can search for a city. To get the result i use this url via ajax:

https://graph.facebook.com/search?q=**sundsvall**&type=adcity&country_list=**se**&access_token=

When I send a request to facebook like that I get a response like this:

{
   "data": [
      {
         "name": "Sundsvall, Sweden",
         "key": 220260543,
         "subtext": "Vasternorrlands Lan, Sweden"
      },
      {
         "name": "Sundsvallen, Sweden",
         "key": 220260542,
         "subtext": "Jamtlands Lan, Sweden"
      }
   ]
}

From that response I get the key and store it in my database table.

My problem

I also want to to the opposite, and get the "name" & "subtext" by providing the "key". You know, "give me the information where key = 220260543".

I know i could store the name and subtext in my database to, but i prefer just to keep the key.

Anyone knows if this is possible? How? Links? I've read the facebook development doc's but haven't found anything yet.

3

There are 3 best solutions below

1
On

If you don't need "subtext"/region, and you're sending full city names (not using in some autocomplete context):

An alternative is to search places (which include cities) rather than adcities. This will yield a Graph ID for the location that can then just be plugged in to graph.facebook.com.

Example query:

https://graph.facebook.com/search?q=sundsvall%20sweden&type=place&access_token=[TOKEN]

This will yield:

{
   "data": [
      {
         "name": "Sundsvall, Sweden",
         "category": "City",
         "location": {
            "latitude": 62.3833,
            "longitude": 17.3
         },
         "id": "110103992352737"
      },
      {
         "name": "Ladyland of Sweden",
         "category": "Local business",
         "location": {
            "street": "Thulegatan 12",
            "city": "Sundsvall",
            "country": "Sweden",
            "zip": "85232",
            "latitude": 62.389481631972,
            "longitude": 17.303409784226
         },
         "id": "170645612982568"
      },
      ...

And as great as Ladyland sounds, you could discard it and anything else in the response not matching "category": "city". Sadly I'm not sure of a way to restrict the results to cities ahead of time, but it seems that cities are consistently sorted highly for this type of query, so the "query then filter" method should work.

Graph ID for Sundsvall, from above result:

https://graph.facebook.com/110103992352737

0
On

According to Facebook's documentation your query is only used for doing auto-complete within the ads API. The key is only specific to a country as well. There is nothing in their documentation showing us devs how to go backwards from the key to the city. There appears to be no FQL table for cities that has a key column. And the graph is still bare bones and has even less.

1
On

You can do so by requesting geometa data like this:

use FacebookAds\Object\TargetingSearch;
use FacebookAds\Object\Search\TargetingSearchTypes;

$result = TargetingSearch::search(
  TargetingSearchTypes::GEOLOCATIONMETA,
  null,
  null,
  array(
    'cities' => array(<city_key>),
  ));

If the returned array at key 'cities' is not empty, then you have a city matching that key:)

The doc to geometa data: https://developers.facebook.com/docs/marketing-api/audiences/reference/targeting-search/#geo-meta