如何获取发送的PHP curl请求的信息 [英] How to get info on sent PHP curl request

查看:542
本文介绍了如何获取发送的PHP curl请求的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调试一个curl请求到webservice的'getToken'端点。

I'm trying to debug a curl request to a webservice 'getToken' endpoint.

我不能100%确信网址和身份验证信息正确写入curl句柄。

I'm not 100% confident that the URL and the auth info is getting written in to the curl handle correctly.

我试图使用 curl_getinfo($ ch,CURLINFO_HEADER_OUT); 捕获发送的请求,没有给我很多信息。有什么方法可以更深入地诊断实际卷曲请求的样子吗?

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?

这里是代码:

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

这一切都可以工作,但每次都返回401我的用户名/ pass我是正确的。

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.

我想知道如果我以某种方式得到URL值错误,或者不发送正确的用户名/密码,但这个信息不会打印在请求数据保存:

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:

HEAD /export/auth HTTP/1.1
Authorization: Basic Y2FpcmRzdW5mYTpENWlAaVM4cw==
Host: webservices.mycompany.com
Accept: */*

您可以看到用户名/密码不记录(我假设为安全)。我认为的端点URL是 host 值加上 HEAD 值的开始,因此 webservices.mycompany.com/export/auth

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?

详细信息语句什么也不打印。不知道为什么会这样!

The "Verbose Information" statement prints nothing. Not sure why on this either!

感谢您的帮助。

编辑:添加详细模式从由于评论者immulatin,调试Curp

added verbose mode from Php - Debugging Curl thanks to commenter immulatin

推荐答案

如果将 CURLINFO_HEADER_OUT 设置为 true curl_getinfo()下的键:

$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']);

这将打印:

GET /bar HTTP/1.1
Authorization: Basic c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk
Host: foo.com
Accept: */*

请注意,auth详细信息是base64编码的:

Note the auth details are base64-encoded:

echo base64_decode('c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk');
// prints: someusername:secretpassword

还要注意,用户名和密码需要百分比 - 编码以转义任何URL保留字符( / & 等),它们可能包含:

Also note that username and password need to be percent-encoded to escape any URL reserved characters (/, ?, &, : and so on) they might contain:

curl_setopt($ch, CURLOPT_USERPWD, urlencode($username).':'.urlencode($password));

这篇关于如何获取发送的PHP curl请求的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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