dongzhanbi0027 2013-12-25 04:08 采纳率: 0%
浏览 115
已采纳

在CLI中静默PHP cURL输出

I am working on a scrapper (in PHP) and use cURL to fetch pages. The script can be run both in CLI & browser. This is my first time working with PHP on CLI and I was trying to make the screen pretty and have a nice data representation like scrape statistics show up.

I am able to generate the output the way I want it, well almost. But with every cURL request the server makes, it also outputs this the extra header information like this :

* 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
...

Function that uses 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;
    }

Now all I want to do is hide this information from the CLI as it does in the browser. Had it been cli curl, i would use -s to shut it up. But I am unable to find an PHP alternative for this. Also, CURLOPT_MUTE is depreciated. All Google gave me was to set CURLOPT_RETURNTRANSFER true, which I already have.

Also I would like to know how can I avoid setting any cookies to avoid tracking.

If it helps in any way I am using

  • OS : Ubuntu
  • Software : PHP5.5
  • Framework : CodeIgniter 3.0-dev
  • Extension: cURL
  • Interface : Command Line (Terminal)
  • 写回答

1条回答 默认 最新

  • douhan8892 2013-12-25 04:29
    关注

    Remove this.

    CURLOPT_VERBOSE => true,
    

    According to php manual

    Set value to

    TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?