Need help with cURL HTTP Code please...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Need help with cURL HTTP Code please...

Post by JAB Creations »

For my CSS validator I'm trying to determine if the validator was reached or not. To test the function I merely echo it. I am pretty sure if I echo it and it returns '0' then that means cURL failed to make the connection to the remote website. I know if it does not return '0' that it reached the validator but there was an error validating the code (most likely they changed the needle in their XHTML haystack).

Any way I'm getting the error curl_getinfo(): 20 is not a valid cURL handle resource on line 21 which is line 14 here. I'm not sure why and this feature isn't exactly well documented. On the up side of things I was able to have my script identify itself by setting a useragent so I don't look like a spammer. :mrgreen:

Code: Select all

function check_css_compliance($css)
{
 $agent = 'JAB Creations Remote CSS Validator 1.0; Validated by: http://'.$_SERVER['SERVER_NAME'];
 $referer = $css;
 $ch = curl_init('http://jigsaw.w3.org/css-validator/validator?uri='.$path_public.'_cache/'.session_id().'.css');
 curl_setopt($ch,CURLOPT_USERAGENT, $agent);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_REFERER,$referer);
 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
 curl_setopt($ch,CURLOPT_TIMEOUT,10);
 $output = curl_exec($ch);
 curl_close($ch);
 
 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 if ($http_code == '200') {$function_result = '200';}
 else if ($http_code == '500') {$function_result = '500';}
madan koshti
Forum Commoner
Posts: 50
Joined: Fri Jun 06, 2008 4:25 am

Re: Need help with cURL HTTP Code please...

Post by madan koshti »

Hi ,

Check your code you have closed the CURL handle first and tryign to get curl info .

curl_close($ch); should be at the last or after getinfo.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Need help with cURL HTTP Code please...

Post by JAB Creations »

Ah HA! Thank you thank you thank you! It makes sense now that someone has brought it to my attention!

Working Example

Code: Select all

<?php
 $agent = 'JAB Creations Remote CSS Validator 1.0; Validated by: http://'.$_SERVER['SERVER_NAME'];
 $ch = curl_init('http://w3.org/');
 curl_setopt($ch,CURLOPT_USERAGENT, $agent);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_REFERER,$referer);
 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
 curl_setopt($ch,CURLOPT_TIMEOUT,10);
 $output = curl_exec($ch);
 $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);
 
echo $httpcode;
?>
A favorite quote comes to mind...
George Bernard Shaw wrote:No question is so difficult to answer as that to which the answer is obvious.
Post Reply