How to fetch Drupal content from external sources with Feeds PHP Fetcher

If you want to fetch content from remote sites that are not supported by standard Feeds fetching plugins, you must probably write custom PHP code to achieve this. Feeds PHP Fetcher simplifies this task by allowing you to enter PHP code directly into Drupal interface, without the complex work of writing custom module and implementing your own Feeds Fetcher plugin.

Installation

Installation of Feeds PHP Fetcher is straightforward: just download the latest version of 7.x-1.x branch (see detailed instructions here), install it and enable. For the examples from this article you will also need:

Example

Step 1: create Importer

Go to Structure -> Feeds Importers and click Add importer. Enter importer name (here we will use Importer1) and press Create.

Step 2: Select and configure fetcher

On the left side you can find Fetcher. Press "Change" option and select "PHP Fetcher". Now save and change PHP fetcher settings ("Settings" link).

Here you should enter your PHP code. In this example we will create a simple array that will contain two records. Each record will contain two keys: key1, key2, with respective values. Your code should return string - in this case, we will serialize the array and later parse it with PHP Array Parser.

Code that will accomplish this is easy:

$arr = array(
0 => array('key1' => 'value1', 'key2' => 'value2'),
1 => array('key1' => 'value3', 'key2' => 'value4')
);
return serialize($arr);

Press "Validate" button to verify if the code returns string containing serialized array. You should get a message:

Your code looks OK. It returned:
a:2:{i:0;a:2:{s:4:"key1";s:6:"value1";s:4:"key2";s:6:"value2";}i:1;a:2:{s:4:"key1";s:6:"value3";s:4:"key2";s:6:"value4";}}

If everything is fine, use Save button to save your configuration.
Note that saving configuration won't re-test your code.

Step 3: Configure Parser

Just change Parser to "PHP Array Parser". It has no configuration options.

Step 4: Configure Processor

We will create nodes based on data returned from Fetcher and parsed by Parser. Therefore we will use "Node processor". Default configuration is OK here - we just have to create proper mappings. Add these records to your mappings:

  • Source: key1, target: title
  • Source: key2, target: body

Step 4: Test it

Now test new importer by going to /import path of your site, selecting Importer1 and pressing Import button. Two nodes should be created with titles: value1 and value3.

Summary

PHP Fetcher allows power-users and developers to easily fetch data from remote sites using arbitrary PHP code. However, if the code could be useful for other users, you should consider building your own fetcher module. PHP Fetcher will be ideal if it's one-time job or a very specific solution.

See PHP Fetcher sandbox project on drupal.org: http://drupal.org/sandbox/lklimek/1671246

Category: 

Add new comment

Markdown

  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <small>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
By submitting this form, you accept the Mollom privacy policy.