PHP - Debugging Curl

后端 未结 8 1322
無奈伤痛
無奈伤痛 2020-11-21 11:35

I\'d like to see what the post fields in the request are before I send it. (For debugging purposes).

The PHP library (class) I am using is already made (not by me),

相关标签:
8条回答
  • 2020-11-21 11:50

    Output debug info to STDERR:

    $curlHandler = curl_init();
    
    curl_setopt_array($curlHandler, [
        CURLOPT_URL => 'https://postman-echo.com/get?foo=bar',
        CURLOPT_RETURNTRANSFER => true,
    
        /**
         * Specify debug option
         */
        CURLOPT_VERBOSE => true,
    ]);
    
    curl_exec($curlHandler);
    
    curl_close($curlHandler);
    

    Output debug info to file:

    $curlHandler = curl_init();
    
    curl_setopt_array($curlHandler, [
        CURLOPT_URL => 'https://postman-echo.com/get?foo=bar',
        CURLOPT_RETURNTRANSFER => true,
    
        /**
         * Specify debug option.
         */
        CURLOPT_VERBOSE => true,
    
        /**
         * Specify log file.
         * Make sure that the folder is writable.
         */
        CURLOPT_STDERR => fopen('./curl.log', 'w+'),
    ]);
    
    curl_exec($curlHandler);
    
    curl_close($curlHandler);
    

    See https://github.com/andriichuk/php-curl-cookbook#debug-request

    0 讨论(0)
  • 2020-11-21 11:52

    You can enable the CURLOPT_VERBOSE option:

    curl_setopt($curlhandle, CURLOPT_VERBOSE, true);
    

    When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR. The output is very informative.

    You can also use tcpdump or wireshark to watch the network traffic.

    0 讨论(0)
  • 2020-11-21 11:58

    You can enable the CURLOPT_VERBOSE option and log that information to a (temporary) CURLOPT_STDERR:

    // CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR, 
    // or the file specified using CURLOPT_STDERR.
    curl_setopt($handle, CURLOPT_VERBOSE, true);
    
    $verbose = fopen('php://temp', 'w+');
    curl_setopt($handle, CURLOPT_STDERR, $verbose);
    

    You can then read it after curl has done the request:

    $result = curl_exec($handle);
    if ($result === FALSE) {
        printf("cUrl error (#%d): %s<br>\n", curl_errno($handle),
               htmlspecialchars(curl_error($handle)));
    }
    
    rewind($verbose);
    $verboseLog = stream_get_contents($verbose);
    
    echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
    

    (I originally answered similar but more extended in a related question.)

    More information like metrics about the last request is available via curl_getinfo. This information can be useful for debugging curl requests, too. A usage example, I would normally wrap that into a function:

    $version = curl_version();
    extract(curl_getinfo($handle));
    $metrics = <<<EOD
    URL....: $url
    Code...: $http_code ($redirect_count redirect(s) in $redirect_time secs)
    Content: $content_type Size: $download_content_length (Own: $size_download) Filetime: $filetime
    Time...: $total_time Start @ $starttransfer_time (DNS: $namelookup_time Connect: $connect_time Request: $pretransfer_time)
    Speed..: Down: $speed_download (avg.) Up: $speed_upload (avg.)
    Curl...: v{$version['version']}
    EOD;
    
    0 讨论(0)
  • 2020-11-21 12:10

    Here is an even simplier way, by writing directly to php error output

    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_STDERR, fopen('php://stderr', 'w'));
    
    0 讨论(0)
  • 2020-11-21 12:13

    To just get the info of a CURL request do this:

    $response = curl_exec($ch);
    
    $info = curl_getinfo($ch);
    var_dump($info);
    
    0 讨论(0)
  • 2020-11-21 12:13

    If you just want a very quick way to debug the result:

    $ch = curl_init();
    curl_exec($ch);
    $curl_error = curl_error($ch);
    echo "<script>console.log($curl_error);</script>"
    
    0 讨论(0)
提交回复
热议问题