Skip to content

Instantly share code, notes, and snippets.

@sabbirshawon
Last active August 1, 2023 17:48
Show Gist options
  • Select an option

  • Save sabbirshawon/a2f6c9208f5ab3bd75983294e5b055b9 to your computer and use it in GitHub Desktop.

Select an option

Save sabbirshawon/a2f6c9208f5ab3bd75983294e5b055b9 to your computer and use it in GitHub Desktop.
put multiple json data using php curl and save response to csv
<?php
$data = file_get_contents("test.json");
$json_a = json_decode($data, true);
$fp = fopen('file.csv', 'w');
foreach($json_a as $rows){
$url = 'http://';
$curl = curl_init($url);
$datas = json_encode($rows,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$data = array('id' => "011",'name' => $datas);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($curl);
//print_r($result);
$myarray = json_decode($result);
$json_array = array();
foreach($myarray AS $row){
if(isset($row->data->id) && isset($row->data->name)){
$json_array['id'] = $row->data->id;
$json_array['name'] = $row->data->name;
fputcsv($fp, $json_array);
}
}
curl_close($curl);
}
fclose($fp);
echo 'Successfully created!';
?>
@Drormusic

Copy link
Copy Markdown

Hi! Can you please explain how to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment