【发布时间】:2013-12-25 04:08:25
【问题描述】:
我正在开发一个 scraper(在 PHP 中)并使用 cURL 来获取页面。该脚本可以在 CLI 和浏览器中运行。 这是我第一次在 CLI 上使用 PHP,我试图让屏幕更漂亮,并有一个很好的数据表示,比如显示抓取统计信息。
我几乎可以按照我想要的方式生成输出。但是对于服务器发出的每个 cURL 请求,它也会像这样输出额外的标头信息:
* 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 已折旧。谷歌给我的只是设置CURLOPT_RETURNTRANSFER true,我已经有了。
我还想知道如何避免设置任何 cookie 以避免跟踪。
如果它对我使用有任何帮助
- 操作系统:Ubuntu
- 软件:PHP5.5
- 框架:CodeIgniter 3.0-dev
- 扩展名:cURL
- 界面:命令行(终端)
【问题讨论】:
-
在此处粘贴脚本中的代码。
-
@chanchal118 我已经包含了我调用来获取 html 的函数。我使用它就像
$html = $this->scrape->getHTML("http://imdb.com');
标签: php codeigniter curl command-line-interface