cURL身份验证在PHP/CLI中失败,但是Wget-CLI和AJAX请求可以工作

cURL身份验证在PHP/CLI中失败,但是Wget-CLI和AJAX请求可以工作,php,curl,wget,Php,Curl,Wget,我正在尝试访问以验证地址,但是我无法使用PHP或CLI通过cURL授权调用 以下Wget CLI调用按预期工作并返回XML: wget --user=username@domain.com.au --password=pass https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street\&suburb=Adelaide\&postcode=5000\&state=SA\&co

我正在尝试访问以验证地址,但是我无法使用PHP或CLI通过cURL授权调用

以下Wget CLI调用按预期工作并返回XML:

wget --user=username@domain.com.au --password=pass https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street\&suburb=Adelaide\&postcode=5000\&state=SA\&country=Australia
以及Wget中的CLI日志:

--2014-12-16 00:33:24--  https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street&suburb=Adelaide&postcode=5000&state=SA&country=Australia
Resolving api.auspost.com.au (api.auspost.com.au)... 54.66.176.138, 54.66.176.138
Connecting to api.auspost.com.au (api.auspost.com.au)|54.66.176.138|:443... connected.
HTTP request sent, awaiting response... 401 Authorization Required
Reusing existing connection to api.auspost.com.au:443.
HTTP request sent, awaiting response... 200 OK
Length: 255 [application/xml]
Saving to: ‘ValidateAddress.xml?addressLine1=31+test+street&suburb=Adelaide&postcode=5000&state=SA&country=Australia.5’
但是,下面的cURL CLI调用没有:

curl -u username@domain.com.au:pass https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street\&suburb=Adelaide\&postcode=5000\&state=SA\&country=Australia -v
卷曲日志:

* Hostname was NOT found in DNS cache
*   Trying 54.66.176.138...
* Connected to api.auspost.com.au (54.66.176.138) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: api.auspost.com.au
* Server certificate: VeriSign Class 3 Extended Validation SSL SGC CA
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
* Server auth using Basic with user 'user@domain.com.au'
> GET /ValidateAddress.xml?addressLine1=3 HTTP/1.1
> Authorization: Basic auth_hash_here
> User-Agent: curl/7.37.1
> Host: api.auspost.com.au
> Accept: */*
>
< HTTP/1.1 401 Authorization Required
最终,我需要通过Guzzle在PHP中调用这个函数,但如果我能让它工作,我将使用cURL!。下面是我尝试过但没有成功的PHP代码

$service_url = 'https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street&suburb=Adelaide&postcode=5000&state=SA&country=Australia';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username@domain.com.au:pass");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

$curl_response = curl_exec($curl);
var_dump($curl_response);
我已经通过Chrome REST客户端成功地运行了该呼叫,并且可以通过Chrome的网络检查器看到该呼叫发送的正确标题

用户名和密码的详细信息是正确的,并且已经验证了多次


有人对如何在PHP中实现这一点有什么建议吗?

Unescape the&'s in$service\u url。@对于相同的401响应,无论是否在$service\u url和cURL CLI call中转义&'s,问题可能与cookies有关-您没有使用它们。@Cheely,就是这样!添加cURL选项CURLOPT_-COOKIEJAR&CURLOPT_-COOKIEFILE有效。
$service_url = 'https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street&suburb=Adelaide&postcode=5000&state=SA&country=Australia';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username@domain.com.au:pass");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

$curl_response = curl_exec($curl);
var_dump($curl_response);