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 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:
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')
Press "Validate" button to verify if the code returns string containing serialized array. You should get a message:
Your code looks OK. It returned:
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.
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