PHP cURL Post requests to Kinvey

by
You have a php web application and want to migrate to a mobile WebApp. You wonder where to store user generated data and how to easily access and update it without going through a lot of programing rings. Maybe reinventing a backend.

Why use Kinvey Backend?

Kinvey has developed their own Api framework to make it easy to communicate with vast Google cloud infrastructure for all types of App developers. You can easily communicate with Kinvey through javascript(using your favorite library - jQuery) but you can also do it using PHP.

First a quick tip

Kinvey's Rest Api documentation is located here: Kinvey Rest Api Docs (http://devcenter.kinvey.com/rest/guides/getting-started#) In the documentation you need to know the "Endpoints". in Api documentation "Endpoints" is what your code talks to in order to get info, save info, upload files to cloud blob storage. Big Tip: Ignore the ":" colons in the docs and use "/" instead with endpoints. Maybe for Api academics colon means something but they can be ignored. Your PHP curl_init url will not use colons. Kinvey-Colon-Confusion



//your datastore endpoints will be as listed here. "appkey" will need to be your App Key listed in your kinvey account
//and "collectionName" is the name of the DataStore you created.
DataStore
Endpoint                            Verb(s)                     Description
/appdata/appkey                                GET                     Simple app handshake
/appdata/appkey/collectionName/id      GET, PUT, POST, DELETE Main resource CRUD
/appdata/appkey/collectionName/?query={..}    GET              Querying
I am jumping ahead here. Let's signup. This won't take long.

Sign Up for a free Starter Account

Sign Up During the signup process you will give the new backend a name and select Api you like to use. Be sure to choose "Rest API". You can change it later. After Signup login and click on "Addons" top left. Yeah, It's hard to notice it's even a menu link but it's where all the goodies are. Kinvey Developer Tools If you get stuck it's very easy to check out Api Console to see what Kinvey is expecting from your PHP code and what Kinvey is sending to your PHP file.

Let's Handshake with Kinvey




//Here is the Kinvey API DOC: http://devcenter.kinvey.com/rest/guides/getting-started#handshake Create this PHP file and browse to it on the web or on your local MAMP or XAMPP
<?php

//tip: user a user account you created in kenvey admin panel. 
//will make things easy when displaying the files later.
        
$username "Place-Kinvey-App-Key-Here";
    
$password "Place-App-Secret-Here";

//set the endpoint in curl_init after https://baas.kinvey.com/
        
$ch curl_init("https://baas.kinvey.com/appdata/Your-App-Key-Here");
    
//You need to send the appropriate header and use base64_encode function to scrable username and password
        
curl_setopt($chCURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: Basic ".base64_encode($username ":" $password),"X-Kinvey-API-Version: 3"));

//cURL after we send something give us a reply.
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

//curl_exec opens the curl connection, grabs the info and saves it in $result2 variable
        
$result2 curl_exec($ch);

//Kinvey sends back JSON and it needs to be decoded using json_decode function in php..more fun functions listed on http://php.net
        
$result2 json_decode($result2true);

//it's a good practice to close what you open.
        
curl_close($ch);

//let's dump what we got back from Kinvey on to page
        
var_dump($result2);    

?>
Enjoy!
Z Data Tech https://www.zdatatech.com/logo/zdatatech-logo.png Last modified: March 30, 2014
Suggested
Advertisement