PHP - Debugging Curl

后端 未结 8 1325
無奈伤痛
無奈伤痛 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: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
    \n", curl_errno($handle), htmlspecialchars(curl_error($handle))); } rewind($verbose); $verboseLog = stream_get_contents($verbose); echo "Verbose information:\n
    ", htmlspecialchars($verboseLog), "
    \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 = <<

提交回复
热议问题