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: [email protected]
Email Address: [email protected]
Email Address: [email protected]

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?

2 Answers 2

2

If the format simply like:

Email Address, [email protected]

Email Address, [email protected]

Email Address, [email protected] 

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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: php.net/manual/en/function.file-put-contents.php
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);
}

2 Comments

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.
That's because you can't read only part of the json input and decode it. json_decode is failing to create an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.