2017-07-08 41 views
-1

這是錯誤,我得到如何停止錯誤消息,如果JSON文件未能打開流

警告:的file_get_contents(https://api.themoviedb.org/3/movie/39740?api_key=522cec78237f49axxxxxxxxxxx6d1e0c834a):未能打開流:HTTP請求失敗! HTTP/1.1 404未找到

現在,您可能是創始人,那個鏈接裏面是什麼?

該網頁包含此行只

{ 「STATUS_CODE」:34, 「STATUS_MESSAGE」: 「您請求的資源找不到」}

所以,我覺得這是一個有效的頁面(我可以在瀏覽器中打開)。我只想讓PHP停止給出這個錯誤,如果它沒有打開流。

這是我的JSON代碼

$response = file_get_contents("https://api.themoviedb.org/3/movie/".$requestsDone."?api_key=522cec782xxxx6f6d1e0c834a"); 
if ($response != FALSE) { 
    $response = json_decode($response, true); 
} 

編輯:這不是重複這個問題的。這個問題的電子郵件和密碼,其中礦是不是

+0

https://stackoverflow.com/questions/3566487/file-get-contents-failed-to-open-stream-unauthorized –

+0

的[文件-GET-內容可能重複失敗打開流未經授權](https://stackoverflow.com/questions/3566487/file-get-contents-failed-to-open-stream-unauthorized) –

+0

它真的是404錯誤嗎?當你在瀏覽器中打開鏈接時,你會得到什麼? –

回答

0

嘗試使用捲曲,以獲得HTTP響應代碼並採取行動時,它的404

<?php 
// create a new cURL resource 
$ch = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "https://api.themoviedb.org/3/movie/39740?api_key=522cec78237f49axxxxxxxxxxx6d1e0c834a"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$responsejson = curl_exec($ch); 
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) { 
    case 200: # OK 
    // do your normal process 
    $response = json_decode($responsejson, true); 
    break; 
    case 404: 
    // do your thing for not found situation 
    $response = 'Not found'; 
    break; 
    case 401: 
    // not allowed 
    $response = 'api key wrong?'; 
    break; 
default: 
    echo 'Unexpected HTTP code: ', $http_code, "\n"; 
} 
} 


// close cURL resource, and free up system resources 
curl_close($ch); 
?> 
1

嘗試coreect或不正確的選項 相關,如果(響應===「正確」){}

+0

不工作bro –

+0

首先檢查這個條件if($ content === FALSE){}。如果仍然在調用file_get_contents()之前使用@,則同樣出現問題:$ content = @file_get_contents($ site); – sekaraja

相關問題