php file_get_content 怎麼擷取錯誤資訊?

來源:互聯網
上載者:User
使用php做跨域代理,使用file_get_content函數發出get請求的時候,對方伺服器設定的是,如果驗證錯誤,則會引發http 422,然後還會返回一個json值。

請問,怎麼擷取這個json值,因為該函數直接返回建立連結失敗,沒有任何值。

回複內容:

使用php做跨域代理,使用file_get_content函數發出get請求的時候,對方伺服器設定的是,如果驗證錯誤,則會引發http 422,然後還會返回一個json值。

請問,怎麼擷取這個json值,因為該函數直接返回建立連結失敗,沒有任何值。

參考 (可能需要搭梯子)

set_error_handler(    create_function(        '$severity, $message, $file, $line',        'throw new ErrorException($message, $severity, $severity, $file, $line);'    ));try {    file_get_contents('www.google.com');}catch (Exception $e) {    echo $e->getMessage();}restore_error_handler();

另外建議使用curl.

有網址, 可以測試嗎?

返回 false ? 試下訪問百度啥的能不能訪問的通

這應用情境好像還沒有碰到過,為嘛要返回422呢,有傳回值就應該是200,然後對json判斷,試試curl

這個函數不行吧,你用curl系列函數試試

function get_contents(){    $content = @file_get_contents("http://segmentfault.com/q/101000000427840111");    var_dump($http_response_header);    var_dump($content);}get_contents();

返回的http 狀態代碼可以這麼擷取
但是如果返回 非 200的話,file_get_contents 會返回 false
可以判斷不同的 http status code ,進行不同的邏輯處理

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.