在变量PHP中保存cURL显示输出字符串

时间:2013-08-13 07:46:51

标签: php variables curl output

是一个在cur变量中保存curl请求输出的选项吗?

因为如果我只保存$ result,我会得到1或没有

<?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);
}
?>

2 个答案:

答案 0 :(得分:28)

您需要将CURLOPT_RETURNTRANSFER选项设置为true。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

答案 1 :(得分:1)

您需要添加curl选项CURLOPT_RETURNTRANSFER:

的设置
  

curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);

使用此功能可以避免输出并使程序继续运行。