rest api: sending a message with an array of deviceids in php using curl


#1

Using php, curl, and rest api having issues messaging a batch of devices.

I can send a message to a single device just fine. But when I try to use an array of ids, I just fail.

Here is a echo of my post fields:

“{
“apikey”: “43yt98yh2t9y234rt************”,
“deviceids”: [“23424”,“24242”,“242442”,“242424”],
“fromnumber”: “16463773838”,
“body”: “AT”
}”

And the “deviceids” field matches exactly whats in the docs. I’m stumped. Here is the current error:

{“success”: false, “error” :“Parsing JSON input to API failed. Check that input is valid.”}


SMS with php
#2

Is that an exact copy of what you’re sending? It looks like those aren’t normal quotes around things.
The error you’re seeing is because something isn’t able to be parsed as valid json and not because the parameters are wrong.


#3

Opps no, I was just outputting a variable with the same structure.

Here is the example from the docs:

curl_setopt($ch, CURLOPT_POSTFIELDS, “{
“deviceid”: 1234,
“deviceids”: [
“12”,
“34”
],
“fromnumber”: “+1-312-555-1212”
}”);

I tried exactly like this with my setup:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $request);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  
  curl_setopt($ch, CURLOPT_POST, TRUE);

  curl_setopt($ch, CURLOPT_POSTFIELDS, "{
    \"apikey\": \"*******92hy4t89y2w\",	
    \"deviceids\": [$strvalue],
    \"fromnumber\": \"7772345423\",
    \"body\": \"$message\"
  }");

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json"
  ));

  $response = curl_exec($ch);
  curl_close($ch);

echo strvalue gives: (made up ids of course…)

\"534\",\"3453\",\"3453\",\"34534\"

I’m stumped…


#4

Hmm, how are you defining strvalue? If you’re calling echo($strvalue); and it’s printing out the backslashes then you may be doing some extra escaping.

How does it work if you do:
$strvalue = "\"100\",\"101\"";

To be honest, the example in the docs is generated by the framework and is pretty generic. If it were me, I’d have PHP generate the JSON for me like this:

$requestObj = [
'apikey' => $apikey,
'deviceids' => ['100', '105'],
'fromnumber' => '16463773838',
'body' => 'AT'
];
$requestStr = json_encode($requestObj);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestStr);

#5

Reuben, Thank you so much finally got it working…


#6

That’s good. Happy to help out