API

(Since 1.7.1) -- import and export pod data
The API supports both a PHP multi-dimensional array and CSV.

Usage

The easiest way to use the API would be by calling the PodAPI() function from within a Pod Page.
The API class is located in /pods/core/PodAPI.class.php and is already included automatically by Pods.

Importing data

Note that attendees is a PICK column, and needs to be an array if multiple values.
<?php
$data = array(
    0 => array(
        'name' => 'My first event',
        'start_date' => '2009-10-30 08:24:30',
        'attendees' => array('Bill Gates', 'Steve Jobs', 'Mario Andretti')
    ),
    1 => array(
        'name' => 'My second event',
        'start_date' => '2012-12-25 06:45:00',
        'attendees' => array('Al Gore', 'Bill Clinton')
    ),
    2 => array(
        'name' => 'My third event',
        'start_date' => '2010-01-20 11:59:99',
        'attendees' => array('Rick Astley')
    )
);
$Record = new PodAPI('event', 'php');
$Record->import($data);
?>

Importing from a file

<?php
$data = file_get_contents('events.csv');
$Record = new PodAPI('event', 'csv');
$Record->import($data);
Columns cannot contain multiple values for this to work.

Exporting data

<?php
// PHP
$Record = new PodAPI('event', 'php'); // Can be "php" or "csv" (default: php)
$data = $Record->export();
var_dump($data); // see the data

// CSV
$Record = new PodAPI('event', 'csv'); 
$data = $Record->export();
$data_keys = array_keys($data[0]); //gets the keys of the array $data to become the column headings.
 
$fp = fopen('file.csv', 'w');                     //Opens "file.csv" in htdocs root.
fputcsv($fp, $data_keys);                         //Output the column names as the first row in the CSV file.
foreach ($data as $line) { fputcsv($fp, $line); } //Loop through $data and output each line to the CSV file.
fclose($fp);
 
echo "Export complete.
n"; echo "Open file.csv

n"; //Download link to get the file. ?>
If your Pod has Pick fields, you can output these by adding the following lines to the foreach loop above before fputcsv:
<?php
    foreach ($data as $line) {                   //Loop through the data line by line
       foreach ($line as &$field) {              //Loop through each line of data by field
          if (is_array($field)) {                //If the field is a PICK column
           $field = 'array(' . implode(', ', $field) . ')';  //Implode the items into a comma separated list wrapped in 'array()'.
          }
       }
?>
More information: fputcsv
See Also