Make cURL output STDERR to file (or string)

后端 未结 6 1955
醉话见心
醉话见心 2020-12-03 11:30

We\'re trying to debug some cURL errors on the server, and I would like to see the STDERR log. Currently, all we can see for our error is \"error code: 7\" and that we can\'

6条回答
  •  执念已碎
    2020-12-03 11:57

    You are making couple mistakes in your example:

    1) you have to call curl_exec() prior to reading from the "verbose log", because curl_setopt() doesn't perform any action, so nothing can be logged prior to the curl_exec().

    2) you are opening $curl_log = fopen("curl.txt", 'w'); only for write, so nothing could be read, even after you write to the file and rewind the internal file pointer.

    So the correct shortened code should look like:

     $url,
        CURLOPT_VERBOSE         => 1,
        CURLOPT_STDERR          => $curl_log,
        CURLOPT_RETURNTRANSFER  => 1
    ));
    
    $response = curl_exec($curl);
    
    rewind($curl_log);
    $output= fread($curl_log, 2048);
    echo "
    ". print_r($output, 1). "
    "; fclose($curl_log); // ... ?>

    NOTE: verbose log could be longer than 2048 bytes, so you could "fclose" the $curl_log after curl_exec() and then read the whole file with for example file_get_contents(). In that case, the point 2) should not be considered as mistake :-)

提交回复
热议问题