我正在尝试访问Australia Post API以验证地址,但是我无法使用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
cURL日志:
* 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
如下所示,使用auth哈希传递'Authorization'标头也会导致401错误。
curl -H 'Authorization: Basic auth_hash_here' https://api.auspost.com.au/ValidateAddress.xml?addressLine1=31+test+street\&suburb=Adelaide\&postcode=5000\&state=SA\&country=Australia -v
最终,我需要在PHP中调用它(理想情况下通过Guzzle调用它,但如果我能使它工作,则需要使用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中使用它的建议吗?