Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Amazon MWS results to Json or Xml and elaborate them

Is there any way to get results of an Amazon MWS request in the Json or Xml format instead of a plain string?

my code is the following:

public function listOrders()
{
    $request = "https://mws.amazonservices.it/Orders/2013-09-01?";
    $request .= $this->getParameterString($this->parameters) . "&Signature=" . $this->calculateSignature($this->calculateStringToSign($this->parameters));

    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $request);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // grab URL and pass it to the browser

    $a = curl_exec($ch);
    echo $a;
    return $a;
}

when $a is shown I see this (a plain string with few possibilities of elaboration):

2016-11-21T22:59:59Z StandardOrder 2016-11-17T06:24:44Z 2016-11-17T18:47:54Z [email protected] 402-2385999-1452355 1 IT Std Domestic Shipped Amazon.it false 0 2016-11-25T22:59:59Z nico 2016-11-20T23:00:00Z EUR 199.00 false 2016-11-17T23:00:00Z APJ6JRA9NG5V4 MFN Other arezzo 3332260766 pratovecchio stia IT 52015 nico via ro 92/94/96 false Standard 2016-11-22T22:59:59Z StandardOrder 2016-11-19T18:35:43Z 2016-11-21T18:14:04Z [email protected] 171-6439117-6622751 1 IT Std Domestic Shipped Amazon.it false 0 2016-11-26T22:59:59Z s 2016-11-22T23:00:00Z EUR 130.00 false 2016-11-20T23:00:00Z APJ6JRA9NG5V4 MFN Other CA 3926624273 Cagliari IT 09126 Samuele civico 244 false Standard 2016-11-28T22:59:59Z StandardOrder 2016-11-24T11:30:20Z 2016-11-24T18:46:12Z [email protected] 404-3098817-1844319 1 IT Std Domestic Shipped Amazon.it false 0 2016-12-02T22:59:59Z ini 2016-11-27T23:00:00Z EUR 110.00 false 2016-11-24T23:00:00Z APJ6JRA9NG5V4 MFN Other latina 3286028770 terracina IT 04019 ...

but if I copy the $request in my browser, what I see is a XML format response.

how can I do?

like image 348
Martina Avatar asked Nov 30 '16 20:11

Martina


1 Answers

The response format documentation suggests that only XML responses will be returned. However, if I'm understanding your OP correctly, you're seeing plain text responses when sent via curl and XML responses when sent via your browser.

If that is correct, then your browser is likely sending a header - probably Accept - that causes Amazon to change the response format. Try adding the following to your cURL setup:

curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Accept: application/xml' ]);

Instead of application/xml you might also try application/json, but again based on the documentation I'm not hopeful that will work.

If adding the Accept header doesn't work, inspect the headers of the browser request and replicate all that seem relevant in your cURL setup. In particular, note that Amazon MWS documents that you should send a User-Agent header, but it'd surprise me if that changes the returned format.

like image 96
bishop Avatar answered Oct 21 '22 02:10

bishop