Last active
August 1, 2023 17:48
-
-
Save sabbirshawon/a2f6c9208f5ab3bd75983294e5b055b9 to your computer and use it in GitHub Desktop.
put multiple json data using php curl and save response to csv
This file contains hidden or 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 | |
$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!'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Can you please explain how to use it?