簡體   English   中英

PHP file_get_contents返回404找不到-忽略錯誤

[英]PHP file_get_contents returns 404 Not Found - Ignore Errors

我遇到了一個我不太了解的奇怪問題。 我的Web服務器通過PHP調用PHP api。 其余的api將根據動詞將請求路由到photos.php文件中的適當函數。 GET,PUT和DELETE請求都可以正常工作,但是下面的POST請求返回404 Not found。 問題是它在瀏覽器中正常運行,或者在我從Postman調用時正常運行。 該文件絕對存在並且具有適當的訪問權限。 我其他的POST請求也沒有此問題。

經過大約5個小時的努力並閱讀了100個站點/文章之后,我遇到了對“ ignore_errors” => true的stream_context_create選項的模糊引用。 我添加該行的那一刻,404消失了,我可以到達api端點。

我不知道怎么可能。 該文件在那兒還是沒有,所以404是一個謊言。 我不想忽略錯誤以使其正常工作。 知道這里發生了什么嗎?

    $apiUrl = 'http://localhost/api/v1/user/john/photos';

    // Setup http request
    $options = ["http" =>
        ["method" => "POST",
            "header" => "Content-Type: application/json",
            "ignore_errors" => true,
            "content" => $data
            ]
        ];
    // Call API
    $apiResponse = file_get_contents($apiUrl, NULL, stream_context_create($options));

這是我收到的錯誤:

 <b>Warning</b>: file_get_contents(http://localhost/api/v1/user/john/photos): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found 

卷曲信息:

Array
(
    [url] => http://localhost/api/v1/user/john/photos
    [content_type] => text/html; charset=UTF-8
    [http_code] => 404
    [header_size] => 214
    [request_size] => 147
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.015
    [namelookup_time] => 0
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 229
    [speed_download] => 15266
    [speed_upload] => 0
    [download_content_length] => 229
    [upload_content_length] => -1
    [starttransfer_time] => 0.015
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 127.0.0.1
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 127.0.0.1
    [local_port] => 53224
)
Header Info: HTTP/1.1 404 Not Found
Date: Fri, 28 Oct 2016 15:14:23 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
X-Powered-By: PHP/5.6.21
Content-Length: 229
Connection: close
Content-Type: text/html; charset=UTF-8

如果您正在尋找為什么file_get_contents在沒有將ignore_errors設置為true的情況下引發錯誤的答案,那么也許這些說明將使您更接近答案。 請注意,這些說明假定php錯誤日志尚未審查或尚無用,並希望能幫助克服這些問題。

  1. 要找到您的php錯誤日志,請將其放在腳本頂部(只是暫時加載該頁面)

    phpinfo(); 死();

  2. 現在,在該頁面加載后,您會看到您的PHP信息,搜索error_log並在主機上找到該目錄。

  3. 如果該值為空,則可能已關閉log_errors或錯誤正在打印到您的Apache錯誤日志中。 如果是這種情況,請嘗試查找apache錯誤日志。

  4. 在錯誤日志可用的情況下,替換您的phpinfo();。 死(); 與這兩個電話。 關閉ignore_errors,然后再次運行文件。

    error_reporting(E_ALL); ini_set(“ display_errors”,1);

  5. 使用錯誤日志,您是否可以找到與file_get_contents調用相關的通知/警告/錯誤,並在未清除問題的情況下將其張貼在此處?

**編輯**

好的,使用curl完整獲取響應和請求標頭,並使用輸出更新您的問題。

ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ...

$response = curl_exec($ch);

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM