如何获取有关已发送 PHP curl 请求的信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17092677/
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
How to get info on sent PHP curl request
提问by user101289
I'm trying to debug a curl request to a webservice 'getToken' endpoint.
我正在尝试调试对 web 服务“getToken”端点的 curl 请求。
I'm not 100% confident that the URL and the auth info is getting written in to the curl handle correctly.
我不是 100% 确信 URL 和身份验证信息是否正确写入 curl 句柄。
I'm trying to use curl_getinfo($ch, CURLINFO_HEADER_OUT);
to capture the sent request, but it doesn't give me much info. Is there a way to get more in depth diagnostics about what the actual curl request looks like?
我试图用来curl_getinfo($ch, CURLINFO_HEADER_OUT);
捕获发送的请求,但它没有给我太多信息。有没有办法更深入地诊断出实际的 curl 请求是什么样的?
Here's the code:
这是代码:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 1); // just getting header to see if we got an auth token
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); // capture the header info
curl_setopt($ch, CURLOPT_VERBOSE, 1); // turn verbose on
// execute the curl request
$rh = fopen("request.txt", "w"); // open request file handle
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_exec($ch); // execute request
$sent_request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
fwrite($rh, $sent_request); // save the request info
fclose($rh);
!rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
This all works as far as it goes, but returns a 401 every time-- the API admin assures me that the username / pass I have is correct.
就目前而言,这一切都有效,但每次都会返回 401——API 管理员向我保证我拥有的用户名/通行证是正确的。
I was wondering if I'm somehow getting the URL value wrong, or not sending the right username / pass, but this info isn't printed in the request data saved:
我想知道我是否以某种方式获取了错误的 URL 值,或者没有发送正确的用户名/通行证,但此信息未打印在保存的请求数据中:
HEAD /export/auth HTTP/1.1
Authorization: Basic Y2FpcmRzdW5mYTpENWlAaVM4cw==
Host: webservices.mycompany.com
Accept: */*
You can see that the username/pass is not recorded (I assume for security). The endpoint URL I think is the host
value plus the start of the HEAD
value, so webservices.mycompany.com/export/auth
?
您可以看到没有记录用户名/通行证(我假设是为了安全)。我认为端点 URL 是host
值加上值的开头HEAD
,所以webservices.mycompany.com/export/auth
?
The "Verbose Information" statement prints nothing. Not sure why on this either!
“详细信息”语句不打印任何内容。也不知道为什么会这样!
Thanks for help.
感谢帮助。
EDIT: added verbose mode from Php - Debugging Curlthanks to commenter immulatin
回答by lafor
If you set CURLINFO_HEADER_OUT
to true
, outgoing headers are available in the array returned by curl_getinfo()
, under request_header
key:
如果设置CURLINFO_HEADER_OUT
为true
,则传出标头在curl_getinfo()
, 下返回的数组中可用request_header
:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://foo.com/bar");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "someusername:secretpassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info['request_header']);
This will print:
这将打印:
GET /bar HTTP/1.1
Authorization: Basic c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk
Host: foo.com
Accept: */*
Note the auth details are base64-encoded:
请注意,身份验证详细信息是 base64 编码的:
echo base64_decode('c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk');
// prints: someusername:secretpassword
Also note that username and password need to be percent-encoded to escape any URL reserved characters (/
, ?
, &
, :
and so on) they might contain:
另外请注意,用户名和密码必须是百分比编码逃避任何URL保留字符(/
,?
,&
,:
和如此),他们可能包含:
curl_setopt($ch, CURLOPT_USERPWD, urlencode($username).':'.urlencode($password));
回答by acetone
You can also use a proxy tool like Charlesto capture the outgoing request headers, data, etc. by passing the proxy details through CURLOPT_PROXY
to your curl_setopt_array
method.
您还可以使用Charles等代理工具通过将代理详细信息传递CURLOPT_PROXY
给您的curl_setopt_array
方法来捕获传出的请求标头、数据等。
For example:
例如:
$proxy = '127.0.0.1:8888';
$opt = array (
CURLOPT_URL => "http://www.example.com",
CURLOPT_PROXY => $proxy,
CURLOPT_POST => true,
CURLOPT_VERBOSE => true,
);
$ch = curl_init();
curl_setopt_array($ch, $opt);
curl_exec($ch);
curl_close($ch);
回答by soulge
curl_getinfo()
must be added before closing the curl handler
curl_getinfo()
必须在关闭 curl 处理程序之前添加
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/bar");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "someusername:secretpassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info['request_header']);
curl_close($ch);
回答by Veeresh Sasalawad
The request is printed in a request.txtwith details
请求打印在request.txt 中,并带有详细信息
$ch = curl_init();
$f = fopen('request.txt', 'w');
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $f,
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
You can also use curl_getinfo() function.
您还可以使用 curl_getinfo() 函数。