1

如果我在终端中运行它(当然使用我的真实 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
)

为什么它在终端中可以正常工作,但在 PHP 中却不行?知道如何让它工作吗?

4

1 回答 1

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 命令。它将向您显示调试打印。它将帮助您匹配命令行和代码的请求标头。

于 2016-09-16T10:42:09.387 回答