Skip to content
Advertisement

cURL to get XML response from an API not working

When I try the url for API endpoint directly in browser, it works and returns XML data, but when I try with cURL, it returns 01 - Unavailable service error, so the issue must be with my cURL code.

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);

Direct URL that works and returns XML data:

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

Advertisement

Answer

Rather than trying to send the url parameters within the headers ( as noted by @CBroe earlier ) you need to create the url with querystring before calling curl_init

$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);

Which then unsurprisingly yields:

string(584) " Sender InvalidUserConnection Unknown user or password.
Details
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement