PHP cURL HTTP CODE return 0
If you connect with the server, then you can get a return code from it, otherwise it will fail and you get a 0. So if you try to connect to "www.google.com/lksdfk" you will get a return code of 400, if you go directly to google.com, you will get 302 (and then 200 if you forward to the next page... well I do because it forwards to google.com.br, so you might not get that), and if you go to "googlecom" you will get a 0 (host no found), so with the last one, there is nobody to send a code back.
Tested using the code below.
<?php
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $html_brand,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo "Return code is {$httpCode} \n"
.curl_error($ch);
} else {
echo "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
Anything wrong with my cURL code (http status of 0)?
Realized that I was having SSL issues. Simply set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false. Works.
PHP cURL Problems: HTTP Code 0
You need to call curl_exec($ch);
before curl_getinfo($ch);
cause this is the actual connection to the server:
also there is no need in the flag CURLOPT_POST
since it's a get call:
// create a new cURL resource
$ch = curl_init();
//for post calls:
//$post = 'a=b&d=c';
//$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=utf-8';
//$headers[] = 'Content-Length: ' . strlen($post);
//for get calls:
$headers = array();
$headers[] = 'Content-type: charset=utf-8';
$headers[] = 'Connection: Keep-Alive';
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.yahoo.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_exec($ch);
$report=curl_getinfo($ch);
print_r($report);
// grab URL and pass it to the browser
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
print curl_error($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Why my code returns an http 0 response instead of expected 200?
I finally found what was the problem: my hosting server firewall has some ports closed, that do not allow a response for some URLs.
Related Topics
Org.JSON.JSONexception: Value <Br of Type Java.Lang.String Cannot Be Converted to JSONobject
Access Joomla 2.5 from External Script to Get Article by Id
Overriding Upload_Max_Filesize
How to Change the Woocommerce_Form_Field HTML Structure
How to Run Multiple Insert Query in SQL Using PHP in One Go
Convert Number to Letter with PHP
How to Password-Protect PHP Page
How to Handle User Input of Invalid Utf-8 Characters
Why, Fatal Error: Class 'Phpunit_Framework_Testcase' Not Found in ...
Get Div Content from External Website
How to Run Laravel Without Artisan
PHP Regular Expression - Filter Number Only
Best Way to Avoid Code Injection in PHP
Codeigniter Send Email with Attach File
Warning: Preg_Replace(): Unknown Modifier 'G'