我正在开发一个 scraper(用 PHP 编写)并使用 cURL 来获取页面。该脚本可以在 CLI 和浏览器中运行。 这是我第一次在 CLI 上使用 PHP,我试图让屏幕变得漂亮,并有一个很好的数据表示,比如显示抓取统计信息。
我几乎可以按照我想要的方式生成输出。但是对于服务器发出的每个 cURL 请求,它还会输出额外的 header 信息,如下所示:
* About to connect() to imbd.com port 80 (#0)
* Trying 123.111.222.333... * connected
> GET /categories/something.html HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20130401 Firefox/21.0
Host: imdb.com
Accept: */*
< HTTP/1.1 200 OK
< Server: nginx/1.4.1
< Date: Wed, 25 Dec 2013 02:17:06 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< X-Powered-By: PHP/5.3.17
< Set-Cookie: mobileType=0%something; expires=Wed, 01-Jan-2014 02:17:06 GMT; path=/; domain=.imdb.com
<
* Connection #0 to host imdb.com left intact
* Closing connection #0
...
Statistics
...
使用 cURL 的函数
public function getHTML($url)
{
$user_agent = "Mozilla/5.0 (Windows NT 5.1; U; zh-cn; rv:1.9.1.6) ...";
$options = Array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_USERAGENT => $user_agent,
CURLOPT_URL => $url,
CURLOPT_VERBOSE => true,
CURLOPT_SSL_VERIFYPEER => false,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
现在我想做的就是从 CLI 中隐藏此信息,就像在浏览器中一样。
如果它是 cli curl,我会使用 -s
来关闭它。但我无法为此找到 PHP 替代方案。此外,CURLOPT_MUTE
已贬值。 Google 给我的只是将 CURLOPT_RETURNTRANSFER
设置为 true,我已经设置好了。
我还想知道如何避免设置任何 cookie 以避免跟踪。
如果对我的使用有任何帮助
- 操作系统:Ubuntu
- 软件:PHP5.5
- 框架:CodeIgniter 3.0-dev
- 扩展名:cURL
- 界面:命令行(终端)
最佳答案
删除它。
CURLOPT_VERBOSE => true,
根据 php manual
将值设置为
TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.
关于php - 在 CLI 中静音 PHP cURL 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768965/