Using Backendless REST API to retrieve data using PHP

I am using the code below in order to try and retrieve the data I have in my Alerts table. Basically, it will list the data on a web page line by line. When I was using Parse, I used the same code, except now I am using the backendless Application ID and Secret Key as well as the API url. It doesn’t look like it’s bringing back any results even though there is data in the Alerts table in backendless. The code is below. When I go to the page that’s running this code I get: "Warning: Invalid argument supplied for foreach() in /home/content/11/10931311/html/backendless/backendlessget.php on line 31 " which seems to indicate that there is no data for the foreach() function to loop through.


<?php


function get_data($url) 


{ 


  $ch = curl_init();


  $timeout = 5;


  curl_setopt($ch,CURLOPT_URL,$url);
  
  curl_setopt( $ch, CURLOPT_HTTPHEADER, array('application-id: xxxxx', 'secret-key: xxxxx', 'Content-Type: application/json', 'application-type: REST')); // -H


  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);


  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);


  $data = curl_exec($ch);


  curl_close($ch);


  return $data;


}


$returned_content = get_data('https://api.backendless.com/v1/data/Alerts');


$data = json_decode($returned_content);
foreach ($data->results as $item) {
    
	echo '&lt;p&gt;'.$item->text;
}


?>

As a follow up to my last post, when I try to run the following cURL commands, I get a message:

{“code”:2002,“message”:"Version is disabled or provided wrong application info

The cURL commands are below (I am using the correct application ID and REST secret key for my app):

curl -H application-id:xxxxx
curl -H secret-key:xxxxx
curl -X GET
curl -v https://api.backendless.com/v1/data/Alerts

Hello Barry.

Please provide us with your application ID.

Regards,
Olga

Here is my app id: 76A9F704-30A1-B509-FF98-9FD7549C0100

Hi Barry,

What you are doing here is sending 4 curl requests instead of one. It should be the following:

curl -H application-id:xxxxx -H secret-key:xxxxx -X GET -v [url=https://api.backendless.com/v1/data/Alerts]https://api.backendless.com/v1/data/Alerts[/url]

As to your code, can you please specify what instruction is on line 31?

Ok thanks. That curl command worked. Regarding line 31, the error line is actually line 30 in the error message not line 31. I cleaned up the code after getting the error and forgot to run it again, so it now says: "Warning: Invalid argument supplied for foreach() in /home/content/11/10931311/html/backendless/backendlessget.php on line 30 "

Apparently, the error is in your PHP script rather than in our SDK. Giving advices on how to debug your code is out of scope of our support, but maybe if you specified what exactly is on line 30 or provided the whole script then I might try to give you some hint.

Ok thanks. Any help is appreciated. Here is the entire script. Line 30 has this: foreach ($data->results as $item){

The entire script is below:

<?php

function get_data($url)

{

$ch = curl_init();

$timeout = 5;

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt( $ch, CURLOPT_HTTPHEADER, array('application-id: xxxxx', 'secret-key: xxxxx', 'Content-Type: application/json', 'application-type: REST')); // -H

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

$data = curl_exec($ch);

curl_close($ch);

return $data;

}

$returned_content = get_data('https://api.backendless.com/v1/data/Alerts');

$data = json_decode($returned_content);
foreach ($data->results as $item) {

echo '&lt;p&gt;'.$item->text;
}

?>

I would try to check whether $data->results is of array type, apparently it’s not. It seems you may get and check the type using gettype( $data->results ) call.

Ok, thanks. I tried using the second parameter in json_decode so now I am using $data = json_decode($returned_content, true) to convert the result to an array. I’ll work on it from here. Thanks for your help.

Hey i would like to know if you could manage to retrieve the data and if you could share your code here as even im interested in finding out.
and i wanted to know how to save the data from a registration form into backendless’s database.
registration form is in php.

@Shaurya_Kane responded to you in slack (general channel)