2

I have a content type: siteinfo.

This content type has multiple fields: text, entity reference etc.

I want to use an external API to automatically fill some fields of the created node.

The API I want to use says that:

API returns consistent and well-structured data in XML & JSON format. Returned data contains parsed fields that can easily be used by your application. This is a RESTful based API.


And this is the API's output:

{
    "status": 1,
    "api_query": "siteinfo",
    "whois_record": {
        "domain_name": "example.com",
        "query_time": "2016-05-05 14:01:12",
        "create_date": "1995-08-14",
        "update_date": "2015-08-14",
        "expiry_date": "2016-08-13",
        "domain_registrar": {
            "iana_id": 376,
            "registrar_name": "RESERVED-INTERNET ASSIGNED NUMBERS AUTHORITY",
            "whois_server": "whois.iana.org",
            "referral_url": "http://res-dom.iana.org"
        },
        "name_servers": [
            "a.iana-servers.net",
            "b.iana-servers.net"
        ],
    },
}

enter image description here


How can I use this external API?

2 Answers 2

2
+50

If there's no complicated authentication/request method for the service (e.g. a simple HTTP GET for http://service.example.com/api/siteinfo/1), then a combination of Feeds, Feeds extensible parsers, & Libraries with the JMESPath library installed works great here.

The Feeds module has an HTTP fetcher that can be used in conjunction with the JSON JMESPath parser that allows you to enter JMESPath queries for each field you want parse and insert/update into a node (e.g. in the parser settings you'd could put "@" (root) as the Context & "whois_record. domain_name" if you wanted to map that object's property to a node field).

In this solution, Feeds is handling the import/update bookkeeping while you're tasked with figuring out the correct queries to parse the data. If you go this path, it's very helpful to study JMESPath syntax while using the Feeds Import Preview module to test your imports before execution.

If you need your URL to vary for each import, you can wrap a programmatic invocation of a Feeds import into a function and call it for each imported URL in a drush eval or some other arbitrary location in your environment. For example:

//$url = Is your imported feed.
//$my_feed = Your imported feed machine name 
//(i.e. the MY_FEED_MACHINE_NAME in http://example.com/import/MY_FEED_MACHINE_NAME)
function MYMODULE_import_feed($url, $my_feed) {
  $feeds_source = feeds_source($my_feed);
  $feeds_config = $feeds_source->getConfigFor($feeds_source->importer->fetcher);
  $feeds_config['source'] = $url;
  $feeds_source->setConfigFor($feeds_source->importer->fetcher, $feeds_config);
  $feeds_source->save();
  $feeds_source->startImport();
}

//Example execution 
function MYMODULE_cron() {
  $import_titles = array('title_1', 'title_2');
  foreach ($import_titles as $title) {
    MYMODULE_import_feed("http://service.example.com/api/siteinfo/$title",'MY_FEED_MACHINE_NAME');
  }
}
3
  • Thanks for your detailed answer. Actually, I'm already using the Simple method (Feeds + Feeds Ex) but there were some problems for me: For each node I had to create a different Feed importer but I don't want to do this. What I want to do is to create a generic Feed importer such as http://service.example.com/api/siteinfo/[NODE:TITLE] and every time I create a node then the Feeds importer works automatically and the node's fields filled with the external data. Is this possible? // Sorry if I mis-explained the situation in the question. Thanks.
    – herci
    Commented May 8, 2016 at 6:16
  • 1
    @herci create a generic Feed importer such as http://service.example.com/api/siteinfo/[NODE:TITLE]I am afraid it is not possible, but you can make it possible by building a custom module and using hook_node_presave() Also, see Updating a field programmatically, hook_node_update to auto fill out the feed url field. PS: Make sure you're usin attach to content type
    – No Sssweat
    Commented May 9, 2016 at 7:26
  • 1
    @herci, I've updated the solution to your particular scenario while addressing generic URL importing.
    – Shawn Conn
    Commented May 10, 2016 at 4:10
1

The no coding, clean hands, solution:

Feeds + Feeds JSONPath Parser

Coding, dirty hands, solution:

Using REST with Webservices you can Get and then Post/Patch. You would also need to comeup with a way to schedule your code so it runs every x amount of time (create a cron job); in contrast, Feeds already has that feature.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.