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

查看:20
本文介绍了使 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 输出详细信息.将输出写入 STDERRCURLOPT_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 并且从不关心权限设置(无论如何它只适用于开发人员).

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() 不会执行任何操作,因此在 curl_exec() 之前不会记录任何内容.

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 字节,因此您可以在 curl_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天全站免登陆