PHP test link Exists code instance sharing, php instance _php tutorial

Source: Internet
Author: User
Tags response code

PHP detects if a link exists code instance sharing, PHP instance


In PHP, check whether a link exists, there are two methods, one is using curl, the other is
Get the HTTP header of the response code, if it is 200 is OK, if it is 404 can not be found, the example is as follows:

1) Use 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 {  

There is a 2nd argument in Get_headers, which is true, the result will be an associative array

2) Use 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 = =)  {  echo "URL Exists"}  } else {  

CURLOPT_NOBODY specifies that only the connection is established and the contents of the entire message are not taken.

http://www.bkjia.com/PHPjc/1123789.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123789.html techarticle PHP detection links exist code instance sharing, php instance in PHP, check whether a link exists, there are two methods, one is to use curl, the other is to get HTTP header response ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.