如果我在终端中运行它(当然使用我真正的api ID),它可以正常工作。
curl -u "MYAPIID":"footastic" "https://myaccountid.wufoo.com/api/v3/forms.json"
我正在尝试使用以下方法在本地服务器(localhost)上获得相同的PHP结果:
$curl = curl_init('https://myaccountid.wufoo.com/api/v3/forms.json');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Form Getter');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD, 'MYAPIID:footastic');
$response = curl_exec($curl);
$resultStatus = curl_getinfo($curl);
if ($resultStatus['http_code'] == 200) {
$result = json_decode($response, true);
} else {
$result = 'Call Failed ' . print_r($resultStatus, true);
}
echo "<pre>";
print_r($result);
echo "</pre>";
它始终使用以下数据返回“调用失败”结果:
Call Failed Array
(
[url] => https://myaccountid.wufoo.com/api/v3/forms.json
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 1
[redirect_count] => 0
[total_time] => 0.216174
[namelookup_time] => 4.1E-5
[connect_time] => 0.215999
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 75.98.93.66
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 192.168.0.9
[local_port] => 54355
)
为什么它在Terminal中工作正常但在PHP中却没有?知道如何让这个工作吗?
答案 0 :(得分:0)
从代码中删除curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
。根据卷曲文件:
CURLAUTH_ANY
This is a convenience macro that sets all bits and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.
如果您在代码中使用curl_setopt($curl, CURLOPT_VERBOSE, true);
,那么您将看到它没有在标头中发送基本身份验证。但是,如果您按照我提到的那样删除该行,您会发现它正在运行。
您还可以使用-v
选项运行curl命令。它将显示调试打印。它将帮助您匹配命令行和代码的请求标头。