Get Amazon MWS results to Json or Xml and elaborate them
Asked Answered
C

2

9

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?

Corot answered 30/11, 2016 at 20:47 Comment(2)
If you are echoing $a out to the browser, could it be that the browser is just hiding the xml tags? Did you view source? Or try var_dump($a)?Deaton
another note is XML will be used as Tag in HTML when every your outputing contents of a download that shows as XML you should run it though htmlentities it will escape the HTML chars for you so you can see them the other option is to view source.Savino
G
3

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.

Glamour answered 5/12, 2016 at 14:54 Comment(0)
W
0

Looking at Amazon MWS documentation they claim:

in response to a HTTP request, Amazon MWS returns an XML file that contains the results of the request. If a request is successful, the response is returned with the data requested. (source).

I think in your case the reason you are receiving a string is the way you are sending your request via cURL. You could try adding:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)');

to your cURL request and see if that fixes the problem. For more detail have a look at this answer.

Wing answered 5/12, 2016 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.