curl_exec printing results when I don't want to
Set CURLOPT_RETURNTRANSFER
option:
// ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
Per the docs:
CURLOPT_RETURNTRANSFER
-TRUE
to return the transfer as a string of the return value ofcurl_exec()
instead of outputting it out directly.
Don't Echo Out cURL
Put this on line 2:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Why does curl_exec print null?
Add
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
php curl_exec returns empty
curl_exec will return false
when the request failed. Adjust your function to this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);
if ($response === false)
$response = curl_error($ch);
echo stripslashes($response);
curl_close($ch);
This way u'll see the curl error
cURL code in PHP dumps output to the page
Use this option to curl_setopt()
:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
This will make curl_exec
return the data instead of outputting it.
To see if it was successful you can then check $result
and also curl_error()
.
Curl automatically display the result?
By default, the curl extension prints out the result.
You need to enable the CURLOPT_RETURNTRANSFER
option, like so:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
After that option is enabled, curl_exec
will return the result, instead.
prevent curl from directly echo/output returned data
You have to add curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
before curl_exec()
.
I don't know why curl doesn't just return by default.
What does curl_exec return?
From the manual:
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
Your code already contains this line (which is good):
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
The 1
means you will receive an explanatory result back from $result = curl_exec ($ch)
instead of just true
or false
.
Your error checking code could therefore look like:
$result = curl_exec ($ch);
if($result === FALSE) {
die(curl_error($ch));
}
You can also check they type of variable returned via var_dump: var_dump($result)
.
Related Topics
Displaying Multiple Lines of a File, Never Repeating
How to Make My PHP Ide Understand Dependency Injection Containers
Session_Start() Creates New Session Every Refresh
PHP File Uploads Doesnot Read $_Files['Image']
PHP "Pretty Print" JSON_Encode
How to Create a Random Hash/String
Have Trouble Installing PHPmyadmin on PHP7 Apache/2.4.7 (Ubuntu)
Fatal Error: Cannot Use Object of Type Stdclass as Array In
Utf8 Filenames in PHP and Different Unicode Encodings
Decoding Numeric HTML Entities via PHP
How to Display All the Images Stored Inside a Database
Php: Possible to Automatically Get All Posted Data and Multiple Checkbox Unchecked
What Is the Overhead of Using PHP Int