PHP檢測連結是否存在的代碼執行個體分享 phpexcel執行個體 php網站執行個體 php執行個體視

來源:互聯網
上載者:User
在PHP中,檢查某個連結是否存在,有兩個方法,一個是使用curl,另外一個是
獲得HTTP的header的響應碼,如果是200的則是OK,如果是404的話就找不到了,例子如下:

1) 使用get_headers:

 <?php $url = "http://www.abc.com/demo.jpg"; $headers = @get_headers($url); if($headers[0] == 'HTTP/1.1 404 Not Found') {  echo "URL not Exists"; } else {  echo "URL Exists"; } ?> 

get_headers中有第2個參數,是true的話,結果將會是個關聯陣列

2) 使用CURL

  <?php $url = "http://www.domain.com/demo.jpg"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_NOBODY, true); $result = curl_exec($curl); if ($result !== false) {  $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  if ($statusCode == 200)  {  echo "URL Exists"  } } else {  echo "URL not Exists"; } ?> 

CURLOPT_NOBODY指定了只是建立串連,而不取整個報文的內容

以上就介紹了PHP檢測連結是否存在的代碼執行個體分享,包括了php,執行個體方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

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