【问题标题】:How to make ob_start() working with curl?如何使 ob_start() 与 curl 一起工作?
【发布时间】:2013-10-19 20:36:08
【问题描述】:

我的班级有以下方法:

public function __construct(){
    $this->handle = curl_init();
}

public function setOptArrayAndExecute(){

    $curlArray = curl_setopt_array(
            $this->handle,
            array(
                    CURLOPT_URL             => $this->getUrl(),
                    CURLOPT_USERAGENT       => $this->getUserAgent(),
                    CURLOPT_COOKIEJAR       => $this->getCookie(),
                    CURLOPT_COOKIEFILE      => $this->getCookie(),
                    CURLOPT_REFERER         => $this->getReferer(),
                    CURLOPT_TIMEOUT         => $this->getTimeOut(),
                    CURLOPT_FOLLOWLOCATION  => true
            )
    );
    ob_start(); //<-- Execution stops here
    curl_exec($this->handle);
    $this->response = ob_get_contents(); 
    curl_close($this->handle);
    ob_end_clean();
    return $this->response;
}

所以我只写了代码的特定部分而不是整个类。 我看了我的 php.ini:输出缓冲设置为“开”。我也激活了错误报告:

error_reporting(E_ALL);
ini_set('display_errors', 1);

我的 PHP 版本是 5.4.3。 该脚本仅在ob_start() 停止,没有任何通知或错误报告... 我不知道我错过了什么或我做错了什么。非常感谢您的帮助。

【问题讨论】:

  • 我认为ob_start 没有问题。删除ob_startob_end_clean,然后重新运行您的代码。对象缓冲导致ob_end_clean(); 之前生成的任何错误或警告被抑制。
  • 是的。你是对的。我忘记了网址的“http://”。感谢您的提示。所以我猜缓冲区无法加载,但我不明白为什么代码总是停止在 ob_start 上工作

标签: php curl methods buffer ob-start


【解决方案1】:

另外,您忘记设置 CURLOPT_CONNECTTIMEOUT,这可能会导致 5 分钟长的连接尝试,这将挂起您的脚本。

【讨论】:

    猜你喜欢
    • 2010-11-23
    • 2018-06-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2021-12-19
    • 2012-05-29
    • 2018-05-17
    • 2014-08-16
    相关资源
    最近更新 更多