Our Blog

Latest news and updates from BinaryOps.io

Using a BinaryOps API with PHP and cURL

With the availability of low-cost PHP hosting and the fact that nearly every development shop has at least one project hosted on PHP, it seems like a great place to start demonstrating how easy it is to use a BinaryOps REST API.

Create a simple REST API

Before we get into any PHP, we need to create our API. I’m going to go over it quickly as it’s not the focus of this post.

  1. Login to the BinaryOps Dashboard, create a new App called “Feedback”
  2. Go to Data -> Entities; Add a new Entity named, “Comments”
  3. Go to Security -> Groups; Add a new Group named “PHP Demo” adding both “Comments Read Role” and “Comments Edit Role” that were created automatically.
  4. Go to Security -> Users; Add a new AppUser named “sample@user.com” with a password and assign the “PHP Demo” group
  5. Go to App -> API Docs; Login with the “sample@user.com” username & password
  6. Scroll down to “Comments”, click it to show both GET & POST ‘/comments’
  7. Expand POST ‘/comments’ and add a few samples that we can search for
  8. Expand GET ‘/comments’ and search for the samples you just added

Take a moment to pat yourself on the back. You’ve just created your first BinaryOps REST API!

Create a new PHP file

Before we get too far in, lets make sure we have cURL installed.

echo function_exists('curl_version');

If your new page doesn’t print “1”, make sure cURL is loaded before continuing.

Now that we’ve confirmed cURL is available, let’s go about logging into the API with our sample@user.com account. To do that we need to send a base-64 encoded username & password in the authorization header to the /login route of our new API.

// Add variables for username, password and the login url
$username = 'sample@user.com';
$password = 'password-goes-here';
$apiUrl = 'https://api.binaryops.io/v1/feedback';

// Create a base64 encoded username and password
$userPass = 'Basic ' . base64_encode($username.':'.$password);

// Initialize cURL setting a few options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . '/login');
curl_setopt($ch, CURLOPT_POST, 1); // use http POST
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Add HTTP headers to the POST request
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'authorization: ' . $userPass;
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

// Execute the "/login" http POST request
$store = curl_exec($ch);

// Error handling for cURL login
if ($store === false) {
   die('Curl error handling login: ' . curl_error($ch));
}
curl_close($ch);

// Parse the JSON reply to our "/login" http POST request
$reply = json_decode($store);
$token = $reply->{'token'};

// We'll use the $token when making subsequent requests of the REST API.

In real code, you’d package that up neatly into a reusable class, but for simplicity let’s keep going forward.

// Create a new cURL GET request that we'll use to query the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0); // use http GET</code>

// Add the authorization HTTP Header and $token to our request
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'authorization: Bearer ' . $token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

// Build an API query string
curl_setopt($ch, CURLOPT_URL, $apiUrl . '/comments?name=Awesome');
$content = curl_exec($ch);

// Error handling for cURL request
if ($content === false) {
die('Curl error handling query: ' . curl_error($ch));
}
curl_close($ch);

// Do something amazing with the JSON response
print $content;

Using cURL to interact with a REST API can be pretty easy and we’ve only scratched the surface with this post. There are many cURL resources on the web and just as many different ways to consume your API data. If you have questions or comments, use the ‘?’ widget in the corner and please send them along!

Tags :  php  curl  api