php下擷取http狀態的實現代碼

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   使用   sp   div   on   

在項目開發中,有時我們需要知道遠端URL地址是否能訪問正常,判斷其正常與否後進行下一步的操作,那麼在PHP中如何擷取遠程HTTP的狀態呢 

逐風整理了兩種方式,大家可以自行參考/使用:

複製代碼 代碼如下:
#方式一
$ch = curl_init(‘http://www.jb51.net‘);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200
curl_close($ch);


方式二

複製代碼 代碼如下:


print_r(
    get_headers(‘http://www.baidu.com‘)
);
#返回以下內容:
/*
Array   
(   
[0] => HTTP/1.1 200 OK   
[1] => Date: Sun, 04 May 2014 03:43:04 GMT   
[2] => Content-Type: text/html; charset=utf-8   
[3] => Connection: Close   
[4] => Vary: Accept-Encoding   
[5] => Set-Cookie: BAIDUID=4977AF4FB1E9A5D13C79939E28D92161:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com   
[6] => Set-Cookie: BDSVRTM=0; path=/   
[7] => Set-Cookie: H_PS_PSSID=4681_1465_5224_6023_4759_6018_6257_6313_6328_6269; path=/; domain=.baidu.com   
[8] => P3P: CP=" OTI DSP COR IVA OUR IND COM "   
[9] => Cache-Control: private   
[10] => Expires: Sun, 04 May 2014 03:42:09 GMT   
[11] => X-Powered-By: HPHP   
[12] => Server: BWS/1.1   
[13] => BDPAGETYPE: 1   
[14] => BDQID: 0x9acb602d00001922   
[15] => BDUSERID: 0   
)   
*/

php下擷取http狀態的實現代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.