Created
October 17, 2013 06:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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