CURL to test if a URL exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    CURL to test if a URL exists

    Hi,

    I have developed a simple form, for site administration purposes so it is behind a user name and password check.

    This form is to enable selected users to add external links to the list of useful links on our site. The form therefore has to take a URL. I have code to test that a URL has been supplied but then the next step is to make sure the URL actually exists.

    I am trying to use CURL for this but so far nothing gives.
    [PHP]
    $lvCurlHandle = curl_init($lcLi nkURL) ;
    $lnHTPPCode = curl_getinfo($l vCurlHandle, CURLINFO_HTTP_C ODE) ;
    $loDebugObject->debugOutput("" ,"","HTTP Code: " . $lnHTTPCode) ;
    if($lnHTTPCode <> 200)
    {
    $lnErrorCount++ ;
    $lcDisplay.= '<p class="warning" >The URL supplied cannot be found</p>' ;
    }
    [/PHP]

    So there's the code. I have been using the URL www.vtpad.co.uk for the variable $lcLinkURL which is coming over from the form.I know the URL exists because it's my test space for projects pre-launch.

    What happens with that URL is the erro rmesasge comes out. I've tried outputting the HTTP code t see if that could shed any light on things but that is empty.

    I figure I'm doing something wrong but re-reading over the manual and other sites I find nothing that sheds some light on the matter.

    Does anyone have a pointer or two for me?

    Many thanks
    nathj
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You could try using the get_headers function instead.
    It will attempt to fetch the headers of the given URL and return them as an array. If the page could not be read, the function returns FALSE and shows a warning.

    Remember that putting a @ in front of a function like that should suppress any warnings it generates.

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by Atli
      Hi.

      You could try using the get_headers function instead.
      It will attempt to fetch the headers of the given URL and return them as an array. If the page could not be read, the function returns FALSE and shows a warning.

      Remember that putting a @ in front of a function like that should suppress any warnings it generates.
      Atli,

      Thank you for the tip. Following on from your pointer I have developed a little function on one of my objects;
      [PHP]
      function checkURL($pcURL , $pcReturnKey)
      {
      // check the supplied URL using the and return the value associated with the specified key
      $laHeaders = @get_headers($p cURL, 1) ; // return the array with keys set

      $lvReturn = $laHeaders[$pcReturnKey] ;
      return $lvReturn ;
      }
      [/PHP]
      All I do is call the method on the object and test the return.

      This now works, I have tested with a good URL and a URL that is not registered.

      Thank yo for your help. Not only I have solved this problem I have learned something new - and its only half 9 in the morning!

      Cheers
      nathj

      Comment

      Working...