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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18203411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:08:39  来源:igfitidea点击:

Save cURL Display Output String in Variable PHP

phpvariablescurloutput

提问by The Masta

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

他们是否可以选择将 curl 请求的输出保存在 php 变量中?

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

因为如果我只保存 $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);
}
?>

回答by poncha

You need to set CURLOPT_RETURNTRANSFERoption to true.

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

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

回答by u2357456

You need to add a setting of curl option CURLOPT_RETURNTRANSFER:

您需要添加 curl 选项 CURLOPT_RETURNTRANSFER 的设置:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

有了这个,您可以避免输出并使程序继续运行。