John Hubler
John Hubler

Reputation: 909

I need help authenticating to an API that uses OAUTH 2, using PHP

I am working in a web application that was coded using procedural PHP. No framework, no MVC, no OOP. It is what it is. At this point in time, recoding to use some sort of framework is not feasible. To my benefit, it is very well organized, and so it's easy to work within. Anyway - they want to add on a Point of Sales system, and have landed on Kounta (kounta.com). Kounta has an API that is RESTful and returns JSON or XML.

I am absolutely brand new to writing applications that integrate with API's, and there are a lot of terms being slung around that I am not quite familiar with.

From my understanding, I need to authenticate myself using oAuth 2.0, and from there, can make server calls to pull data from their server.

The first piece of that is what I need help with.

I have my client ID and client secret. I am just not sure what to do with them, and how to pass them to their server via script in order to receive a token, so that I can then make those server calls.

The Kounta API Documentation can be found here (http://www.kounta.com/documentation/).

Any help that anybody could provide would be greatly appreciated. At this point, I am not sure where to even get started.

Here is the code that I am currently using. This code returns an error asking me to identify myself to Kounta.

<?php
  $url = "https://api.kounta.com/v1/companies/5678/orders.json?created_gte=2013‑06‑01";
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
  $json = curl_exec($ch);
  if(!$json) {
    echo curl_error($ch);
  }
  curl_close($ch);
  print_r(json_decode($json));
?>

Upvotes: 3

Views: 766

Answers (1)

Jason Bassett
Jason Bassett

Reputation: 1291

I'm not too familiar with Kounta, but I recently did a project which required me to fetch data from Instagram based on a users ID.

To do this, I used CURL. Instagram has a pretty open API and even some examples. So it wasn't too hard for me to figure it out.

See below for my example:

<?php
  function fetchData($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close($ch); 
    return $result;
  }
  $result = fetchData("https://api.instagram.com/v1/users/<user>/media/recent/?access_token=<access_token>&count=4");
  $result = json_decode($result);
  foreach ($result->data as $post) {
    echo '<div class="col-sm-3">
    <a href="'.$post->images->standard_resolution->url.'" data-lightbox="instagram" data-title="'.$post->caption->text.' '.implode(' #',$post->tags).'">
    <img src="'.$post->images->thumbnail->url.'" alt="" /></a>
    </div>';
  }
?> 

What this does is simply connect to Instagrams API with a user ID and access token inside the URL and returns a JSON Array.

My only purpose for showing you this is because this might be exactly what you will need to do going forward with Kounta, in some form or fashion. I have not reviewed their API or anything so I can't say for sure. However, I'm more than certain CURL will be your best bet.

I would advise you to fully read through their API once more and see what options they have or maybe even see if they offer any examples to help you get started.

EDIT: You mentioned they they return JSON and XML. This is good to know because that more than likely means you can use CURL to get data from their databases.

Let me know if you have any questions.

Upvotes: 1

Related Questions