PHP - 调试卷曲

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3757071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:59:51  来源:igfitidea点击:

PHP - Debugging Curl

phpdebuggingcurl

提问by Matthew

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

我想在发送之前查看请求中的 post 字段是什么。(用于调试目的)。

The PHP library (class) I am using is already made (not by me), so I am trying to understand it.

我正在使用的 PHP 库(类)已经制作好了(不是我制作的),所以我想了解它。

As far as I can tell, it uses curl_setopt()to set different options like headers and such and then it uses curl_exec()to send the request.

据我所知,它用于curl_setopt()设置不同的选项,如标头等,然后curl_exec()用于发送请求。

Ideas on how to see what post fields are being sent?

关于如何查看正在发送的帖子字段的想法?

采纳答案by netom

You can enable the CURLOPT_VERBOSEoption:

您可以启用该CURLOPT_VERBOSE选项:

curl_setopt($curlhandle, CURLOPT_VERBOSE, true);

When CURLOPT_VERBOSEis set, output is written to STDERRor the file specified using CURLOPT_STDERR. The output is very informative.

CURLOPT_VERBOSE被设置时,输出写入STDERR或使用指定的文件CURLOPT_STDERR。输出信息非常丰富。

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

您还可以使用 tcpdump 或 wireshark 来观察网络流量。

回答by hakre

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

您可以启用该CURLOPT_VERBOSE选项并将该信息记录到(临时)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:

您可以在 curl 完成请求后阅读它:

$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 extendedin 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:

可通过 获得更多信息,例如有关上次请求的指标curl_getinfo。此信息也可用于调试 curl 请求。一个用法示例,我通常会将其包装到一个函数中:

$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;

回答by Mario S

Here is a simpler code for the same:

这是一个更简单的代码:

   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_STDERR, $fp);

where $fp is a file handle to output errors. For example:

其中 $fp 是输出错误的文件句柄。例如:

   $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');

( Read on http://curl.haxx.se/mail/curlphp-2008-03/0064.html)

(阅读http://curl.haxx.se/mail/curlphp-2008-03/0064.html

回答by Micha?l R

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

这是一种更简单的方法,通过直接写入 php 错误输出

curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, fopen('php://stderr', 'w'));

回答by Andrew

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

要获取 CURL 请求的信息,请执行以下操作:

$response = curl_exec($ch);

$info = curl_getinfo($ch);
var_dump($info);

回答by Nick Spicer

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>"

回答by mario

Another (crude) option is to utilize netcat for dumping the full request:

另一个(粗略)选项是利用 netcat 转储完整请求:

nc -l -p 8000 -w 3 | tee curldbg.txt

And of course sending the failing request to it:

当然,将失败的请求发送给它:

curl_setup(CURLOPT_URL, "http://localhost/testytest");

Notably that will always hang+fail, since netcat won't ever construct a valid HTTP response. It's really just for inspecting what really got sent. The better option, of course, is using a http request debugging service.

值得注意的是,这将始终挂起+失败,因为 netcat 永远不会构建有效的 HTTP 响应。这实际上只是为了检查真正发送的内容。当然,更好的选择是使用http 请求调试服务

回答by Serhii Andriichuk

Output debug info to STDERR:

将调试信息输出到 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

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