cURL 从 API 获取 XML 响应无效

cURL to get XML response from an API not working

当我直接在浏览器中为 API 端点尝试 url 时,它有效并且 returns XML 数据,但是当我尝试使用 cURL, 它 returns 01 - Unavailable service 错误,所以问题一定出在我的 cURL 代码上。

PHP:

$url = "https://ws.fr.shopping.rakuten.com/stock_ws";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "Accept: application/xml",
    "action: export",
    "login: mylogin",
    "pwd: mytokenxxx",
    "version: 2018-06-29"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

直接 URL 有效 returns XML 数据:

https://ws.fr.shopping.rakuten.com/stock_ws?action=export&login=mylogin&pwd=mytokenxxx&version=2018-06-29

与其尝试在 headers 中发送 url 参数(如@CBroe 之前所述),不如在调用 [=12= 之前使用查询字符串创建 url ]

$url = "https://ws.fr.shopping.rakuten.com/stock_ws";
$headers = array(
    "Accept: application/xml"
);
$args=array(
    "action"=>"export",
    "login"=>"mylogin",
    "pwd"=>"mytokenxxx",
    "version"=>"2018-06-29"
);
$url=sprintf('%s?%s',$url,http_build_query( $args ));

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);

var_dump($resp);

然后不出所料地产生:

string(584) " Sender InvalidUserConnection Unknown user or password.
Details