2017-08-08 85 views
-1

在使用API​​和PHP時,我是一個完整的noob。 我想將以下代碼轉儲結果存儲到一個名爲books.json 的文件中,我不知道該怎麼做。任何幫助是極大的讚賞!從紐約時間API輸出到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); 
+0

對不起,我downvote,因爲它似乎在你的資源如何打開和PHP編寫一個文件至少搜索作出任何努力。 – Fabien

+0

已經在這裏回答了https://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php – TehSphinX

回答

0

看一看file_put_contents:

file_put_contents($path_to_file, $result); 
+0

謝謝。 file_put_contents正在創建該文件,但它是空白的。我錯過了什麼嗎? <?php //由LucyBot構建。 www.lucybot.com $ curl = curl_init(); curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1); $ query = array( 「api-key」=>「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