1

I'm trying to use Mailchimp's Export API to generate a CSV file of all members of a given list. Here' the documentation and the example PHP code they give:

$apikey = 'YOUR_API_KEY';
$list_id = 'YOUR_LIST_ID';
$chunk_size = 4096; //in bytes
$url = 'http://us1.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$list_id;

/** a more robust client can be built using fsockopen **/
$handle = @fopen($url,'r');
if (!$handle) {
  echo "failed to access url\n";
} else {
  $i = 0;
  $header = array();
  while (!feof($handle)) {
    $buffer = fgets($handle, $chunk_size);
    if (trim($buffer)!=''){
      $obj = json_decode($buffer);
      if ($i==0){
        //store the header row
        $header = $obj;
      } else {
        //echo, write to a file, queue a job, etc.
        echo $header[0].': '.$obj[0]."\n";
      }
      $i++;
    }
  }
  fclose($handle);
}

This works well for me and when I run this file, I end up with a bunch of data in this format:

Email Address: xdf@example.com
Email Address: bob@example.com
Email Address: gerry@example.io

What I want to do is turn this into a CSV (to pass to a place on my server) instead of echoing the data. Is there a library or simple syntax/snippit I can use to make this happen?

jonmrich
  • 4,233
  • 5
  • 42
  • 94

2 Answers2

2

If the format simply like:

Email Address, xdf@example.com

Email Address, bob@example.com

Email Address, gerry@example.io 

is what you after, then you can do:

$handle = @fopen($url,'r');
$csvOutput = "";
if (!$handle) {
  echo "failed to access url\n";
} else {
  $i = 0;
  $header = array();
  while (!feof($handle)) {
    $buffer = fgets($handle, $chunk_size);
    if (trim($buffer)!=''){
      $obj = json_decode($buffer);
      if ($i==0){
        //store the header row
        $header = $obj;
      } else {
        //echo, write to a file, queue a job, etc.
        echo $header[0].', '.$obj[0]."\n";
        $csvOutput .= $header[0].', '.$obj[0]."\n";
      }
      $i++;
    }
  }
  fclose($handle);
}
$filename = "data".date("m.d.y").".csv";
file_put_contents($filename, $csvOutput);

The variable $csvOutput contains the CSV format string.

Peter Peng
  • 1,910
  • 1
  • 27
  • 37
  • This is a perfect start. Thanks. Can you recommend a library that can take the string and create a CSV file? I've been hunting and trying a few, but can't get it to work. – jonmrich Aug 14 '15 at 00:41
  • You don't need a library to write string to a file. Try this: $filename = "data".date("m.d.y").".csv"; file_put_contents($filename, $csvOutput); (See last two lines of the code snippet) A link for PHP file_put_contents function: http://php.net/manual/en/function.file-put-contents.php – Peter Peng Aug 14 '15 at 00:52
1

This ones on me. From now on you might want to actually read some documentation instead of copying and pasting your way through life. other's will not be as nice as i am. here's a list of file system functions from the php website. http://php.net/manual/en/ref.filesystem.php Getting the file output to the desired csv format is an exercise left to the reader.

$apikey = 'YOUR_API_KEY';
$list_id = 'YOUR_LIST_ID';
$chunk_size = 4096; //in bytes
$url = 'http://us1.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$list_id;

/** a more robust client can be built using fsockopen **/
$handle = @fopen($url,'r');
if (!$handle) {
    echo "failed to access url\n";
} else {
    $i = 0;
    $header = array();
    $output = ''; //output buffer for the file we are going to write.
    while (!feof($handle)) {
        $buffer = fgets($handle, $chunk_size);
        if (trim($buffer)!=''){
            $obj = json_decode($buffer);
            if ($i==0){
            //store the header row
            $header = $obj;
            } else {
                //write data into our output buffer for the file
                $output .= $header[0].': '.$obj[0]."\n";
                }
            $i++;
        }
    }
    fclose($handle);
    //now write it to file
    $path = '/path/to/where/you/want/to/store/file/';
    $file_name = 'somefile.csv';
    //create a file resource to write to.
    $fh = fopen($path.$file_name,'w+');
    //write to the file
    fwrite($fh,$output);
    //close the file
    fclose($fh);
}
r3wt
  • 4,642
  • 2
  • 33
  • 55
  • Thanks for providing this. I've actually tried a few things similar to this and keep ending up with an empty CSV, as is the case with your code. – jonmrich Aug 14 '15 at 00:33
  • That's because you can't read only part of the json input and decode it. json_decode is failing to create an object. – Heyflynn Oct 26 '16 at 18:26