By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

CURL to test if a URL exists

nathj
Expert 100+
P: 938
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($lcLinkURL) ;
$lnHTPPCode = curl_getinfo($lvCurlHandle, CURLINFO_HTTP_CODE) ;
$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
Oct 13 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
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.
Oct 14 '08 #2

nathj
Expert 100+
P: 938
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($pcURL, 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
Oct 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.