1

Im try export json result to csv and save data as file, im try with something like this

$getFile = file_get_contents('JSON_URL');
$json_obj = json_decode($getFile);
$fp = fopen('/home/xxxx/public_html/xxxx/api/export/tmp/file.csv', 'w');
    foreach ($json_obj as $row) {
        fputcsv($fp, $row);
    }
fclose($fp);

but seems not working

Here's an example json format for link above

[
    {key:value,key:value...}
...]
3
  • 1
    What is happening when you do that? And what did you expect to happen? Commented Aug 11, 2013 at 14:24
  • in what way is it not working? are you getting any errors? is it creating a file at all? is it creating it empty, or with incorrect data? or mis-formatted? Commented Aug 11, 2013 at 14:25
  • Just get error in error_log Invalid argument supplied for foreach() , im change title of post, i need to export data to CSV file Commented Aug 11, 2013 at 14:27

1 Answer 1

1

in order for your code to work as expected, try decoding the json object as an associative array. This is done by passing a boolean true to the 2nd param of json_decode

$json_obj = json_decode($getFile, true);
Sign up to request clarification or add additional context in comments.

1 Comment

could you point me at a way to do this if the JSON is being posted to the PHP through a form? I am not a PHP dev but am trying to cobble this together through scraps of code.

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.