Php 从纽约时间API到JSON文件的输出

Php 从纽约时间API到JSON文件的输出,php,json,Php,Json,在使用API和PHP时,我是一个十足的noob。 我希望将以下代码转储到名为books.json的文件中 我不知道怎么做。非常感谢您的帮助 // 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, CURLO

在使用API和PHP时,我是一个十足的noob。 我希望将以下代码转储到名为books.json的文件中 我不知道怎么做。非常感谢您的帮助

// 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);

查看文件内容:

file_put_contents($path_to_file, $result);

很抱歉,我投了反对票,因为您似乎没有努力搜索有关如何用PHP打开和编写文件的资源。这里已经回答了,谢谢。文件内容正在创建文件,但为空。我错过什么了吗?