TN888
TN888

Reputation: 7739

cURL erros while asking for HTTP code

I have this code now (after appyling fixes from this question: Check if website is available in fastest possible way)

foreach($links as $link_content)
{
                $handle = curl_init(LINK_BASE.$link_content);
                curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
                curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' );
                curl_setopt( $c, CURLOPT_HEADER, 1 );
                curl_setopt( $c, CURLOPT_NOBODY, true );
                $content = curl_exec ($handle);
                curl_close ($handle); //warning there!!!
                $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
                if($httpCode != 200)
                    continue; //if not, go to next link

}

Error log:

Warning: curl_getinfo(): 17 is not a valid cURL handle resource in C:\xampp\htdocs\index.php on line 82

Warning: curl_getinfo(): 18 is not a valid cURL handle resource in C:\xampp\htdocs\index.php on line 82

Warning: curl_getinfo(): 19 is not a valid cURL handle resource in C:\xampp\htdocs\index.php on line 82

I am not sure what is casuing this warning. Additionally, code doesn't work as expected. I continues loop in every case, also when website is available and returning code 200. Can you give me any tips?

Upvotes: 0

Views: 149

Answers (1)

ceejayoz
ceejayoz

Reputation: 180024

You're calling your cURL handle $handle:

$handle = curl_init(LINK_BASE.$link_content);

but then you're trying to use $c instead in your curl_setopt calls:

curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );

Upvotes: 1

Related Questions