CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Address API: Get country name instead of country ID?
Pages: [1]

Author Topic: Address API: Get country name instead of country ID?  (Read 1733 times)

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Address API: Get country name instead of country ID?
December 11, 2014, 02:07:26 am
Hi all,

When I search for an address using the api like this:

Code: [Select]
drush cvapi Address.getsingle sequential=1 id=1761
I get:

Code: [Select]
    {
    "id":"1761",
    "contact_id":"2",
    "location_type_id":"0",
    "is_primary":"1",
    "is_billing":"0",
    "street_address":"Hoefslagstraatje 2",
    "city":"Gent",
    "postal_code":"9000",
    "country_id":"1020",
    "manual_geo_code":"0",
    "is_error":0
    }

Now I wonder whether it is possible to make the API returning the name of the country (Belgium) instead of the country id (1020). I tried the following:

Code: [Select]
drush cvapi Address.getsingle sequential=1 return="id,street_address,postal_code,city,country" id=1761

That didn't work out:

Code: [Select]
Array
(
    [id] => 1761
    [street_address] => Hoefslagstraatje 2
    [city] => Gent
    [postal_code] => 9000
)

I tried chaining: (Couldn't do this with drush, so I used the http-api:)

Code: [Select]
wget -qO- 'http://localhost/dev2/sites/all/modules/civicrm/extern/rest.php/?api_key=myapikeyi&key=mysitekey&entity=Address&action=get&json={"sequential":1,"id":1761,"api.Country.get":{}}

It seemed that I had to grant my api user 'administer CiviCRM' permissions, for this to do something. But it didn't work either: I got all countries (limited to the first 25 records):

Code: [Select]
{"is_error":0,"version":3,"count":1,"id":1761,"values":[{"id":"1761","contact_id":"2","location_type_id":"0","is_primary":"1","is_billing":"0","street_address":"Hoefslagstraatje 2","city":"Gent","postal_code":"9000","country_id":"1020","manual_geo_code":"0","api.Country.get":{"is_error":0,"version":3,"count":25,"values":[{"id":"1001","name":"Afghanistan","iso_code":"AF","region_id":"4","is_province_abbreviated":"0"},{"id":"1002","name":"Albania","iso_code":"AL","region_id":"1","is_province_abbreviated":"0"},{"id":"1003","name":"Algeria","iso_code":"DZ","region_id":"3","is_province_abbreviated":"0"},{"id":"1004","name":"American Samoa","iso_code":"AS","region_id":"2","is_province_abbreviated":"0"},{"id":"1005","name":"Andorra","iso_code":"AD","region_id":"1","is_province_abbreviated":"0"},{"id":"1006","name":"Angola","iso_code":"AO","region_id":"5","is_province_abbreviated":"0"},{"id":"1007","name":"Anguilla","iso_code":"AI","region_id":"2","is_province_abbreviated":"0"},{"id":"1008","name":"Antarctica","iso_code":"AQ","region_id":"1","is_province_abbreviated":"0"},{"id":"1009","name":"Antigua and Barbuda","iso_code":"AG","region_id":"2","is_province_abbreviated":"0"},{"id":"1010","name":"Argentina","iso_code":"AR","region_id":"2","is_province_abbreviated":"0"},{"id":"1011","name":"Armenia","iso_code":"AM","region_id":"1","is_province_abbreviated":"0"},{"id":"1012","name":"Aruba","iso_code":"AW","region_id":"2","is_province_abbreviated":"0"},{"id":"1013","name":"Australia","iso_code":"AU","region_id":"4","is_province_abbreviated":"0"},{"id":"1014","name":"Austria","iso_code":"AT","region_id":"1","is_province_abbreviated":"0"},{"id":"1015","name":"Azerbaijan","iso_code":"AZ","region_id":"1","is_province_abbreviated":"0"},{"id":"1016","name":"Bahrain","iso_code":"BH","region_id":"3","is_province_abbreviated":"0"},{"id":"1017","name":"Bangladesh","iso_code":"BD","region_id":"4","is_province_abbreviated":"0"},{"id":"1018","name":"Barbados","iso_code":"BB","region_id":"4","is_province_abbreviated":"0"},{"id":"1019","name":"Belarus","iso_code":"BY","region_id":"1","is_province_abbreviated":"0"},{"id":"1020","name":"Belgium","iso_code":"BE","region_id":"1","is_province_abbreviated":"0"},{"id":"1021","name":"Belize","iso_code":"BZ","region_id":"2","is_province_abbreviated":"0"},{"id":"1022","name":"Benin","iso_code":"BJ","region_id":"5","is_province_abbreviated":"0"},{"id":"1023","name":"Bermuda","iso_code":"BM","region_id":"2","is_province_abbreviated":"0"},{"id":"1024","name":"Bhutan","iso_code":"BT","region_id":"4","is_province_abbreviated":"0"},{"id":"1025","name":"Bolivia","iso_code":"BO","region_id":"2","is_province_abbreviated":"0"}]}}]}

So I have two questions: is there a way to get the name of the country from the address API, or do I need to do a 2nd api call? And if so, is this a bug, or is this expected behaviour?

EranSch

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 3
    • Personal Blog
  • CiviCRM version: 4.5.X
  • CMS version: 4.X
  • MySQL version: 5.5.X
  • PHP version: 5.5.X
Re: Address API: Get country name instead of country ID?
December 29, 2014, 01:11:49 pm
It looks like you're missing parameters for the chained call in the REST query... Instead of:

Code: [Select]
...&json={"sequential":1,"id":1761,"api.Country.get":{}}
You'd want to pass a country id:

Code: [Select]
...&json={"sequential":1,"id":1761,"api.Country.get":"api.Country.getsingle":{"id":"$value.country_id"}}
I'm a WP user so there's no drush to speak of for me however the command works fine with the WP equivalent (WP-CLI)...

Code: [Select]
wp cv api Address.getsingle sequential=1 id=1 api.Country.getsingle={"id":"$value.country_id"}

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Address API: Get country name instead of country ID?
December 30, 2014, 02:26:37 am
Thank you, EranSch. This was exactly what I was looking for.  :)

EranSch

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 3
    • Personal Blog
  • CiviCRM version: 4.5.X
  • CMS version: 4.X
  • MySQL version: 5.5.X
  • PHP version: 5.5.X
Re: Address API: Get country name instead of country ID?
December 30, 2014, 06:07:51 am
Glad I could help! As someone who's forged their understanding of the API through trial and error, I'm happy to pass along some help!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Address API: Get country name instead of country ID?

This forum was archived on 2017-11-26.