11

I have an external web service, which can be accessed using a HTTP call (with parameters passed as query string), and that returns the result in XML format.

I would like to use Drupal to access that web service (with the desired parameters), and display the result using Views.

These are a sample query and the result obtained from the web service I am using.

Query: http://localhost/query.php?cy=in&q=abcd&cat=22
Result:

<?xml version="1.0"?>
<results>
<Jobs>
    <Job ID="111">
        <Title>Test one</Title>
        <Summary>Test one summary</Summary>
        <DateUpdated Date="2011-04-04 18:03:00">4/4/2011</DateUpdated>
        <Location>
            <Country>India</Country>
            <City>Delhi</City>
        </Location>
        <CompanyName>Test Company</CompanyName>
    </Job>
    <Job ID="222">
        <Title>Test two</Title>
        <Summary>Test two summary</Summary>
        <DateUpdated Date="2011-04-04 18:03:00">4/4/2011</DateUpdated>
        <Location>
            <Country>India</Country>
            <City>Chennai</City>
        </Location>
        <CompanyName>Test company</CompanyName>
    </Job>
</Jobs> 
</results>  

3 Answers 3

4

The advised solutions are based on getting the data into the database first, then showing it in Views.

An alternative is querying the service directly from Views, by using a custom query backend. Checkout http://drupal.org/node/958270 for links to custom backends which you can use as an example for writing your own (it's not a lot of work)

5
  • Sure, but the advantages of getting the data into the db first are more than just simplicity, it also means caching the data. Querying a local datastore is always faster than an external service.
    – Alex Weber
    Commented Apr 6, 2011 at 15:31
  • But, drupal.org/node/958270 is with using Views3.0. Any way to not use Views 3.0 and also without need to save the data into db? Just consume the webservice realtime and display data on page.
    – user939
    Commented Apr 10, 2011 at 22:50
  • Of course, that goes without saying. Commented Apr 10, 2011 at 23:51
  • No. Views 2 is very near end-of-life, so all new features are in Views 3. Commented Apr 10, 2011 at 23:52
  • @Alex: You can use Views 3 with an external Webservice and still cache the results.
    – s.Daniel
    Commented Apr 4, 2013 at 10:54
3

The Feeds module can pull in content and turn it into nodes. Feeds XPath Parser will allow you to parse the xml file and map tags to fields. You can then use Views to display the content as normal.

1

I'm sure there's a more elegant solution out there, but I've done this in the past with twitter feeds and it works just fine. Here's my take on it:

  • Create a content type to store your results and set up fields as necessary to match the content you will be consuming
  • Create a module that implements hook_cron() and periodically consumes the web service and stores results as nodes

The second step can be achieved by programatically creating the node instances:

$node = new stdClass();
$node->title = 'title';
$node->my_custom_field['und'][0]['value'] = 'my custom value';
// fill all your other fields
node_save($node);
  • The best way to avoid duplication when creating the nodes programatically would be to compare the ID (or timestamp, or whatever) of the web service's results to the most recent entry in the database:
SELECT fdfpi.field_picture_id_value FROM field_data_field_picture_id fdfpi INNER JOIN node n ON fdfpi.entity_id = n.nid AND fdfpi.entity_type = 'node' WHERE n.type = 'picture' ORDER BY n.created DESC LIMIT 1

The above example can seem kinda confusing but its not really: I created a content type "picture" that has a custom field "picture_id". Its just a manual way to retrieve the ID for the most recently created node of that type.

  • So now that you have a specific content type that you automatically populate on cron time... profit! You can use Views to display the data any which way you please because its in the form of regular ol' Drupal nodes! :)

Hope this helps!

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.