question

nff6201 avatar image
nff6201 asked ·

Help please when sending message via Amazon Device Messaging API using php

The first step get access_token it's OK.It returns like: HTTP/1.1 200 OK Date: Wed, 29 May 2013 08:04:04 GMT x-amzn-RequestId: 5496e38d-c836-11e2-add2-3d6f274bd6a1 Content-Type: application/json Content-Length: 287 Vary: Accept-Encoding,User-Agent {"scope":"messaging:push","token_type":"bearer","expires_in":3600,"access_token":"Atc|MQEBILZJXq_hvWzCYHBYyZwNH3I3crc6V0j0_5vxYFBvlDLzdoKMQ3NfpE4nBmOLHGJwI_h2CwTpw2KnEarYuoOluV-N-T-LY5S3IcGPiuo_zTvr5Sh003FgY7b08J4VkHKGr3Fw5JsDHu2csZVz-nTZ3EwfowVMJqLakGCzQp9J7PJXpxsp5xrd6bVDWZrKW1f4tk0"} But when I try to send message I always receive Could not parse XML The code below: $accessToken = "accessToken"; $registrationId = "amzn1.adm-registration.v2.Y29tLmFtY……"; $payload = array(); $data = array(); $data['firstKey'] = 'firstValue'; $data['secondKey'] = 'secondValue'; $payload['data'] = $data; $payload['consolidationKey'] = "ADM_Enqueue_Sample"; $payload['expiresAfter'] = 86400; $payloadString = json_encode($payload); $postData = array( "payload" => $payloadString ); $admUrl = " https://api.amazon.com/messaging/registrations/$registrationId/messages"; $headers = array(); $headers['content-type'] = "application/json"; $headers['accept'] = "application/json"; $headers['X-Amzn-Type-Version'] = "com.amazon.device.messaging.ADMMessage@1.0"; $headers['X-Amzn-Accept-Type'] = "com.amazon.device.messaging.ADMSendResult@1.0"; $headers['Authorization'] = "Bearer $accessToken"; $ch = curl_init(); $res= curl_setopt ($ch, CURLOPT_URL,$admUrl); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); $result = curl_exec ($ch); curl_close($ch); echo $result Do you have some idea? Thank you very much.
amazon device messaging
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered ·
Hi Nff6201, Thank you for the post. I think you are setting post data in the request incorrectly. According to ADM documentation ( https://developer.amazon.com/sdk/adm/sending-message.html) the payload data should be send as a string, not as object or array. e.g, in java, OutputStream os = conn.getOutputStream(); os.write(payloadString.getBytes(), 0, payloadString.getBytes().length); // please refer the above link In php, you are doing this, $payloadString = json_encode($payload); $postData = array( "payload" => $payloadString ); . . . curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); This is wrong. It should be, curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadString); No need to define a named array. Hope this help. All the best.
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Me Girl Team avatar image
Me Girl Team answered ·
Hi I just followed your example code, replacing all the relevant information. I still got the same error. "Could not parse XML". Is there something else that I am not setting up correctly? I just switch on messaging. Just wondering if there is a time delay of some kind before I can start sending messages. Cheers Kevin
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered ·
Hi Kevin, Could you post your modified code?
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Me Girl Team avatar image
Me Girl Team answered ·
Hi, Finally after many hours of testing. I hope this helps someone else out. $payload = array(); $data = array(); $data['from'] = 'Kevin'; $data['message'] = 'It\'s ok'; $data['timeStamp'] = '2013-06-06 17:07:54.371000'; $payload['data'] = $data; $payload['consolidationKey'] = "sync"; $payload['expiresAfter'] = 86400; $payloadString = json_encode($payload); $headers = array( 'Accept: application/json', 'Content-Type: application/json', 'X-amzn-type-version: com.amazon.device.messaging.ADMMessage@1.0', 'X-amzn-accept-type: com.amazon.device.messaging.ADMSendResult@1.0', 'Authorization: Bearer '.$accessToken ); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadString); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); Seems like the above style works for setting the headers using curl. No more dreaded XML problem Also, once I had that working initially what came back was {"message":null} Nothing really changed and it "suddenly started working".. Not sure why. However it's been 24hrs since and the above code is working. I hope this helps someone else out. Kevin
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered ·
Hi Nff6201, Thanks for sharing your working code. I did not find any significant changes in the above two code snippets other than sending $payloadString in CURLOPT_POSTFIELDS instead of $postData. When we tried to do the same, our code worked properly from the first run as expected. Any way, good to know that your problem is resolved.
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.