从cron作业输出中隐藏cURL信息


Hide cURL info from cron job output

对于我的大多数cron工作,我使用php /path/to/file.php > /dev/null,它只允许我在有流式输出到stderr的情况下接收电子邮件。除了执行cURL请求之外,这对任何事情都非常有效。

使用CURLOPT_RETURNTRANSFER = true,如果我从浏览器运行文件,它不会输出,但通过cron作业,我会收到一封电子邮件,其中包含请求的详细信息,包括连接尝试和发送/接收的标头。

有没有一种方法可以将此输出管道传输到stdout,或者最好将其从输出流中完全删除,因为如果我从浏览器运行它,我也不想/不需要看到这些信息。

谢谢你抽出时间。


代码:

getCURL("www.example.com", array(CURLOPT_COOKIEFILE => COOKIES));

function getCURL($url, $opt = array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "someuseragent");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIES);
    curl_setopt($ch, CURLOPT_ENCODING, "");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt_array($ch, $opt);
    $response_raw = curl_exec($ch);
    $header = str_replace("'r'n'r'n", "", substr($response_raw, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE)));
    foreach (explode("'r'n", $header) as $i => $line){
        if ($i === 0){
            $headers['http_code'] = $line;
        } else {
            list ($key, $value) = explode(': ', $line);
            $headers[$key] = $value;
        }
    }
    $body = substr($response_raw, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
    curl_close($ch);
    return array(
        "header" => $headers,
        "body" => $body,
        "cookies" => COOKIES);
}

要将CURLOPT_VERBOSE设置为false