-1

I am a complete noob when working with APIs and PHP. I want to have the following code dump results into a file called books.json and I have no idea how to do it. Any help is greatly appreciated!

// Built by LucyBot. www.lucybot.com
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$query = array(
  "api-key" => "MY APRI KEY"
);
curl_setopt($curl, CURLOPT_URL,
  "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json" . "?" . http_build_query($query)
);
$result = json_decode(curl_exec($curl));
echo json_encode($result);
Solidjim
  • 1
  • 1
  • Sorry I downvote because it seems you made no effort in at least searching for resources on how to open and write a file in PHP. – Fabien Aug 08 '17 at 14:36
  • Has already been answered here https://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php – TehSphinX Aug 08 '17 at 14:46

1 Answers1

0

Have a look at file_put_contents:

file_put_contents($path_to_file, $result);
Kisaragi
  • 2,198
  • 3
  • 16
  • 28
  • Thanks. file_put_contents is creating the file, but it is blank. Am I missing something? "MY KEY" ); curl_setopt($curl, CURLOPT_URL, "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json" . "?" . http_build_query($query) ); $result = json_decode(curl_exec($curl)); json_encode($result); file_put_contents("books2.json", $result); ?> – Solidjim Aug 08 '17 at 15:32