如何获取有关已发送 PHP curl 请求的信息 [英] How to get info on sent PHP curl request
问题描述
我正在尝试调试对网络服务getToken"端点的 curl 请求.
我不能 100% 确信 URL 和身份验证信息是否正确写入 curl 句柄.
我正在尝试使用 curl_getinfo($ch, CURLINFO_HEADER_OUT);
来捕获发送的请求,但它没有给我太多信息.有没有办法更深入地诊断出实际的 curl 请求是什么样的?
代码如下:
$ch = curl_init($url);curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");curl_setopt($ch, CURLOPT_HEADER, 1);//只是获取标头以查看我们是否获得了身份验证令牌curl_setopt($ch, CURLOPT_FILE, $fh);curl_setopt($ch, CURLOPT_NOBODY, 1);curl_setopt($ch, CURLINFO_HEADER_OUT, 1);//获取头部信息curl_setopt($ch, CURLOPT_VERBOSE, 1);//开启详细//执行卷曲请求$rh = fopen("request.txt", "w");//打开请求文件句柄$verbose = fopen('php://temp', 'rw+');curl_setopt($ch, CURLOPT_STDERR, $verbose);curl_exec($ch);//执行请求$sent_request = curl_getinfo($ch, CURLINFO_HEADER_OUT);fwrite($rh, $sent_request);//保存请求信息fclose($rh);!rewind($verbose);$verboseLog = stream_get_contents($verbose);echo "详细信息:
", htmlspecialchars($verboseLog), "
";
就目前而言,这一切都有效,但每次都会返回 401——API 管理员向我保证我拥有的用户名/通行证是正确的.
我想知道我是否以某种方式获取了错误的 URL 值,或者没有发送正确的用户名/密码,但此信息未打印在保存的请求数据中:
HEAD/export/auth HTTP/1.1授权:基本 Y2FpcmRzdW5mYTpENWlAaVM4cw==主机:webservices.mycompany.com接受: */*
您可以看到没有记录用户名/通行证(我假设是为了安全).我认为端点 URL 是 host
值加上 HEAD
值的开头,所以 webservices.mycompany.com/export/auth
?
详细信息"语句不打印任何内容.也不知道为什么会这样!
感谢您的帮助.
从 Php - Debugging Curl 添加了详细模式,感谢评论者 immulatin
如果将 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']);
这将打印:
GET/bar HTTP/1.1授权:基本 c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk主办方:foo.com接受: */*
请注意,身份验证详细信息是 base64 编码的:
echo base64_decode('c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk');//打印:someusername:secretpassword
另请注意,用户名和密码需要进行百分比编码以转义任何 URL 保留字符(/
、?
、&
、:
等等)它们可能包含:
curl_setopt($ch, CURLOPT_USERPWD, urlencode($username).':'.urlencode($password));
I'm trying to debug a curl request to a webservice 'getToken' endpoint.
I'm not 100% confident that the URL and the auth info is getting written in to the curl handle correctly.
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?
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:
<pre>", htmlspecialchars($verboseLog), "</pre>
";
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.
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: */*
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!
Thanks for help.
EDIT: added verbose mode from Php - Debugging Curl thanks to commenter immulatin
If you set CURLINFO_HEADER_OUT
to true
, outgoing headers are available in the array returned by curl_getinfo()
, under request_header
key:
$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:
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:
curl_setopt($ch, CURLOPT_USERPWD, urlencode($username).':'.urlencode($password));
这篇关于如何获取有关已发送 PHP curl 请求的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!