I've never worked with curl before so I'm trying to just set up something basic: query google.com and write it. This is what I have:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($curl_handle);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($curl_handle);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error
";
} else {
if(empty($buffer)) {
echo "No data received.";
} else {
echo "Data received: $buffer
";
}
}
?>
However, it's returning "No data received" every single time.
This is in Kubuntu 11.04 x64 with PHP version 5.3.5 and cURL version 7.21.3.
You are mixing your variable names $ch
and $curl_handle
. You can't do this. Be consistent ($ch
is pretty standard):
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch); // Changed variable name here
$curl_errno = curl_errno($ch); // Removed duplicate line here
curl_close($ch); // Changed variable name here
?>
Fix this
$buffer = curl_exec($curl_handle);
to
$buffer = curl_exec($ch);
and also it should be,
curl_close($ch)
not $curl_handle. Btw, closing curl is not really necessary for what ur doing. But its a good practice nevertheless.