Save cURL Display Output String in Variable PHP

is their an option to save the outpout of a curl request in a php variable?

Because if i only save the $result i get a 1 or nothing

<?php
$url='http://icanhazip.com';
$proxy=file ('proxy.txt');
$useragent='Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';

for($x=0;$x<count($proxy);$x++)
{
$ch = curl_init();
//you might need to set some cookie details up (depending on the site)
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_URL,$url); //set the url we want to use
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, $proxy[$x]);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); //set our user agent
$result= curl_exec ($ch); //execute and get the results
print $result; //display the reuslt
$datenbank = "proxy_work.txt"; 
$datei = fopen($datenbank,"a");
fwrite($datei, $result);  
fwrite ($datei,"\r\n"); 
curl_close ($ch);
}
?>

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You need to set CURLOPT_RETURNTRANSFER option to true.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Solution 2

You need to add a setting of curl option CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

With this you can avoid the output and make the programme continue running.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply