Support Topics Documentation Slack YouTube Blog

PHP SDK for Backendless 4


(Daniel Schuring) #1

Does the PHP SDK work with Backendless 4?

We have a client that is migrating to Backendless 4, but part of their service requires a PHP SDK (currently running on their v 3 app), which we now need to connect to their migrated v 4 app.


(Sergey Chupov) #2

Hi Daniel,

No, we’ve now ported PHP SDK to Backendless 4. But you can use REST API - it’s pretty easy using PHP.
Also our PHP SDK on GitHub is open-source and we would appreciate and support anyone who would bother porting it to v4 :slight_smile:


(Daniel Schuring) #3

Ok, can you point me int the right direction for using the REST API with php?

Or a sample?


(Daniel Schuring) #4

For example, here’s a basic query using the SDK:

$query = new BackendlessDataQuery();
$query->setWhereClause(“major = '”.$test_major."’ AND minor = ‘".$test_minor."’");
$result_collection = Backendless::$Persistence->of( ‘BeaconRegister’ )->find( $query )->getAsObjects();
if (empty($result_collection)) {
$valid = true;
}


(Sergey Chupov) #5

This is actually out of Backendless scope, so we don’t have any examples in the doc.

I saw many our customers use PHP cURL - http://php.net/manual/en/book.curl.php. And our REST API docs are available here: https://backendless.com/docs/rest/doc.html. Using our REST API from PHP cURL library (or any other PHP HTTP library) shouldn’t be any different from any other HTTP-based API.


(Daniel Schuring) #6

This quickstart guide provides some background, but only mentions how to use a users token for the calls. Is that necessary? How can we make REST calls without using a user’s token. Can’t we use the REST secret key?

https://backendless.com/mobile-developers/quick-start-guide-for-rest-api/


(Sergey Chupov) #7

User token are REST API key (API key is what was called secret key in v3) are the different things. The user token is used to designate that the request comes from a logged in user, and used to identify that user. API key is included into URL, so you always use it implicitly, and it is only used to identify the type of client.


(Daniel Schuring) #8

I’ve been able to run some GET requests with curl to the REST api.

But POSTing is failing.


(Sergey Chupov) #9

The Content-Type seems to be OK, it’s application/json as expected. Could you please show the code where you set the request body?


(Daniel Schuring) #10

$object = [];
$object[‘code’] = $valid_code;
$object[‘uuid’] = $valid_uuid;
$object[‘major’] = $valid_major;
$object[‘minor’] = $valid_minor;
$data_string = json_encode($object);

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => ‘https://api.backendless.com/’.$app_id.’/’.$app_rest_key.’/data/BeaconRegister’,
CURLOPT_CUSTOMREQUEST => ‘POST’,
CURLOPT_POSTFIELDS => $data_string
));
$resp = curl_exec($curl);


(Sergey Chupov) #11

Looks like here is the same problem you’re running into: http://php.net/manual/en/curl.examples-basic.php#117009


(Daniel Schuring) #12

I’m not passing it as an array.

$data_string is a json string. json_encode converts an array to a json string.


(Sergey Chupov) #13

Yes, you’re right. You can use some proxy tool (e.g. Charles Proxy) to see what actually is sent in the request. Apparently either the Content-Type is not application/json or the content is not in json format somehow.


(Daniel Schuring) #14

Can you please edit my message and XXX out the APP ID and REST KEY?

Thank you


(Mark Piller) #15

done