使cURL输出STDERR到文件(或字符串) [英] Make cURL output STDERR to file (or string)

查看:200
本文介绍了使cURL输出STDERR到文件(或字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试调试服务器上的一些cURL错误,我想查看STDERR日志。目前,我们可以看到我们的错误是错误代码:7,我们无法连接到目标服务器。我们已经联系了主机,并制定了特殊规则,打开我们需要的端口,我们暂时忽略了证书。

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't connect to target server. We have contacted the host and made special rule to open the port we need and we're even ignoring the certificate for the time being.

仍然,我们无法连接。

Still, we can't connect. I need to debug this, but I can't see any pertinent information on my end.

提到VERBOSE和STDERR的行是最重要的,我需要调试这个,但我看不到任何相关的信息。认为。没有写入$ curl_log。我究竟做错了什么?遵循手册逻辑,这应该是正确的...

The lines mentioning "VERBOSE" and "STDERR" are the most important, I think. Nothing is written to $curl_log. What am I doing wrong? Following the manuals logic, this should be correct...

<?php
$curl = curl_init();
$curl_log = fopen("curl.txt", 'w');
$url = "http://www.google.com";

curl_setopt_array($curl, array(
    CURLOPT_URL             => $url,        // Our destination URL
    CURLOPT_VERBOSE         => 1,           // Logs verbose output to STDERR
    CURLOPT_STDERR          => $curl_log,   // Output STDERR log to file
    CURLOPT_SSL_VERIFYPEER  => 0,           // Do not verify certificate
    CURLOPT_FAILONERROR     => 0,           // true to fail silently for http requests > 400
    CURLOPT_RETURNTRANSFER  => 1            // Return data received from server
));

$output = fread($curl_log, 2048);
echo $output; // This returns nothing!
fclose($curl_log);

$response = curl_exec($curl);
//...restofscript...
?>

从PHP手册: http://php.net/manual/en/function.curl-setopt.php

CURLOPT_VERBOSE TRUE可输出详细信息。将输出写入STDERR
CURLOPT_STDERR 输出错误而不是STDERR的替代位置。

CURLOPT_VERBOSE TRUE to output verbose information. Writes output to STDERR CURLOPT_STDERR An alternative location to output errors to instead of STDERR.

它也不是一个权限问题,我已设置文件和脚本权限为777在服务器端,我的本地客户端是Windows,从来没有关心权限设置(它只是为dev)。

It is not a permission issue either, I have set file and script permissions to 777 on server side and my local client is windows and has never cared about permission settings (it's only for dev anyway).

推荐答案

您在您的示例中出现了几个错误:

You are making couple mistakes in your example:

1)在读取详细日志之前必须调用 curl_exec(),因为 curl_setopt()

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),您将打开 $ curl_log = fopen(curl.txt,'w'); 仅用于写入,因此即使在写入文件并回滚内部文件指针后,也不会读取任何内容。

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:

<?php
$curl = curl_init();
$curl_log = fopen("curl.txt", 'rw'); // open file for READ and write
$url = "http://www.google.com";

curl_setopt_array($curl, array(
    CURLOPT_URL             => $url,
    CURLOPT_VERBOSE         => 1,
    CURLOPT_STDERR          => $curl_log,
    CURLOPT_RETURNTRANSFER  => 1
));

$response = curl_exec($curl);

rewind($curl_log);
$output= fread($curl_log, 2048);
echo "<pre>". print_r($output, 1). "</pre>";
fclose($curl_log);

// ...

?>

注意:详细日志可能超过2048个字节, fclosecurl_exec()之后的$ curl_log,然后用例如file_get_contents()读取整个文件。
在这种情况下,点 2)不应被视为错误: - )

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 :-)

这篇关于使cURL输出STDERR到文件(或字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆