【发布时间】:2013-06-10 04:04:21
【问题描述】:
我正在尝试调试对 web 服务“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); // 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——API 管理员向我保证我拥有的用户名/密码是正确的。
我想知道我是否以某种方式获取了错误的 URL 值,或者没有发送正确的用户名/密码,但此信息未打印在保存的请求数据中:
HEAD /export/auth HTTP/1.1
Authorization: Basic Y2FpcmRzdW5mYTpENWlAaVM4cw==
Host: webservices.mycompany.com
Accept: */*
你可以看到用户名/密码没有被记录(我假设是为了安全)。我认为端点 URL 是 host 值加上 HEAD 值的开头,所以 webservices.mycompany.com/export/auth?
“详细信息”语句不打印任何内容。也不知道为什么会这样!
感谢您的帮助。
编辑:感谢评论者 immulat,从 Php - Debugging Curl 添加了详细模式
【问题讨论】:
-
您可以将verbose设置为true:stackoverflow.com/questions/3757071/php-debugging-curl
-
我要检查的第一件事是我会用最少的参数发出一个更简单的请求,并手动输入我的所有变量值(url、登录名、密码)。像这样的连线错误通常是由格式错误的输入引起的,例如额外的空格等。
-
我觉得这个老帖子可能有用stackoverflow.com/questions/866946/…>
标签: php web-services curl