Question

For most of my cron jobs, I use php /path/to/file.php > /dev/null which allows me to only get emails if there is output streamed to stderr. This works great for everything apart from when there are cURL requests being executed.

Using CURLOPT_RETURNTRANSFER = true, it won't output if I run the file from a browser, but through cron jobs, I get an email full of the request details including connection attempts and sent/received headers.

Is there a way to either pipe this output to stdout, or preferably, remove it from the output streams entirely, as I don't want/need to see this information if I run it from a browser either.

Thanks for your time.


Code:

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);
}
Was it helpful?

Solution

you want to set CURLOPT_VERBOSE to false

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top