Skip to content

Instantly share code, notes, and snippets.

@sebmih
Created October 17, 2013 06:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sebmih/7020250 to your computer and use it in GitHub Desktop.
Save sebmih/7020250 to your computer and use it in GitHub Desktop.
A working script that creates a CSV file using the data from a cURL request.
<?php
$url = 'http://sendgrid.com/';
$user = 'schoologysendgridapi';
$pass = '*********';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'email' => 'ahandler@stewie.schoologize.com',
'date' => '1',
'days' => '30',
'limit' => '1000'
);
print_r($params);
$request = $url.'api/blocks.get.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, 0);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
//Create CSV file
$decoded_file = json_decode($response, true);
$fp = fopen('file.csv', 'w');
foreach ($decoded_file as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment