Backendless Support
 
Answered

PHP SDK for Backendless 4

Does the PHP SDK work with Backendless 4?

https://github.com/Backendless/PHP-SDK

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.

Leave a Comment

Comments (14)

photo
1

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 :)

photo
1

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

Or a sample?

photo
1

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.

photo
photo
1

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;

}

photo
1

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/

photo
1

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.

photo
photo
1

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

But POSTing is failing.

photo
1

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?

photo
1

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

Thank you

photo
1

done

photo
photo
1

$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);

photo
1

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

photo
1

I'm not passing it as an array.

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

photo
1

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.

photo