【问题标题】:Read content using curl使用 curl 读取内容
【发布时间】:2011-06-13 12:07:11
【问题描述】:

使用 Curl 我正在向服务器发送一些帖子数据。这是我正在获取的 Header 响应

HTTP/1.1 100 继续 HTTP/1.1 200 OK Content-Length: 30 Content-Type: text/html;charset=ISO-8859-1 Server: Microsoft-IIS/7.0 X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1 设置-Cookie:JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXX;路径=/网关;安全 P3P:CP="CAO PSA OUR" X-Powered-By:ASP.NET 日期:2011 年 1 月 22 日星期六 00:07:25 GMT

我如何阅读其余内容?

【问题讨论】:

    标签: php curl


    【解决方案1】:

    也许您忘记在 curl 调用中设置 CURLOPT_RETURNTRANSFER 选项? 设置后,响应的内容由 curl_exec() 函数本身返回。

    这是我的一个工作项目的代码:

        $options = array(
                CURLOPT_SSL_VERIFYPEER => false, 
    
                CURLOPT_RETURNTRANSFER => $this->return_content,     // return web page
                CURLOPT_HEADER         => false,    // don't return headers, they are processed through callback
                CURLOPT_FOLLOWLOCATION => $this->follow_redirects,     // follow redirects
                CURLOPT_ENCODING       => "",       // handle all encodings
                CURLOPT_USERAGENT      => $this->user_agent, // who am i
                CURLOPT_CONNECTTIMEOUT => $this->connect_timeout,      // timeout on connect
                CURLOPT_TIMEOUT        => $this->request_timeout,      // timeout on response
                CURLOPT_BINARYTRANSFER => true,
                );
        $extraheaders = array();            
    
        $res = curl_init( $url );
        curl_setopt_array( $res, $options );
        $content = curl_exec( $res );
    

    【讨论】:

    • 我已将 CURLOPT_RETURNTRANSFER 选项设置为 1,但仍只能获取标题内容。
    • 那么 curl_exec 函数返回什么?空字符串?
    • 它返回“HTTP/1.1 100 Continue HTTP/1.1 200 OK Content-Length: 30 Content-Type: text/html;charset=ISO-8859-1 Server: Microsoft-IIS/7.0 X-技术支持:Servlet 2.5;JBoss-5.0/JBossWeb-2.1 Set-Cookie:JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXXX;路径=/thegateway;安全 P3P:CP="CAO PSA OUR" X-Powered-By:ASP.NET 日期:周六, 2011 年 1 月 22 日 00:07:25 GMT"
    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 2015-10-11
    • 2018-09-09
    • 2011-04-05
    • 2011-08-16
    • 2012-01-14
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多