Objective

Main objective of this blog post is to give you an idea about how to use Basecamp API using Laravel in Web

 

 

Step 1 Introduction

  • API :- Application Programming Interface

- It’s a particular set of rules that software programs follow to communicate with each other.
- Using API we are able to interact with someone else’s code and affect our public methods and properties.
- API expresses a Software Component in terms of its operations, inputs, outputs.
- An API specifies a set of functions or routines that accomplish a specific task or are allowed to interact with a specific software component.

Example Site: http://www.sitepoint.com/build-rest-resources-laravel/ 

basecamp

 

Step 2 What is Basecamp?

- Basecamp is a project management application. Everyone who works at Basecamp interact with customers on a regular basis. That's why we need to be careful about who we hire.
- Basecamp works hard to make sure every employee is given the tools, and support to do the best work of their careers.
- Basecamp’s at home on Chrome (Mac, PC, iPhone/iPad, Android), Safari (Mac, iPhone/iPad), Firefox (Mac, PC), and IE 9+ (PC, Surface). Basecamp also integrates effortlessly with email.
- There is no need of any IT department to carry on work in basecamp.
- Many Project Management tools are available in basecamp.

 

 

2.1 Basecamp API :-

Basecamp provides following APIs
https://github.com/basecamp/bcx-api/

new-basecamp-api

All the Basecamp APIs are available here.
Here, we will try one example.

- https://basecamp.com/Here-Your-Company-Id/api/v1/projects.json
- The above API link provides the project data for logged in user.
- Then we need to retrieve the JSON data.
- To use any type of API, we first need to check whether any data is returned or not?
- One should Use Chrome Extension DHC or Advanced Rest Client for API testing.

 

2.2 Using DHC

set-an-authorization

Using DHC will return following type of JSON data:

[
{
"id": 8147534,
"name": "Learning - Web",
"description": "",
"archived": false,
"is_client_project": false,
"created_at": "2015-01-21T14:45:07.000+05:30",
"updated_at": "2015-03-07T12:42:43.000+05:30",
"trashed": false,
"color": "000099",
"draft": false,
"template": false,
"last_event_at": "2015-03-07T12:42:43.000+05:30",
"starred": false,
"url": "https://basecamp.com/Your-Company-Id/api/v1/projects/8147534.json",
"app_url": "https://basecamp.com/Your-Company-Id/projects/8147534"
}
]

- Now convert this received json data into an Array using json_decode() function (available in php).

Let’s start to implement this API in laravel.

First, we need to gain some knowledge about Laravel.
- Laravel is a free, open source PHP web application framework, designed for the development of MVC (model–view–controller) web applications.
- Laravel is a clean and classy framework for PHP web development. Development should be a creative experience that you enjoy.
- Laravel provide a model, controller, migration to make Web Application.
- Restful controllers provide an optional way for separating the logic behind serving HTTP GET and POST requests.

Follow below steps to create laravel project.

 

Step 3 Write Following command in your terminal.

create-project-laravel

 

Step 4 Next move path to project in terminal and write following code to start a server

php-artisan-server

 

Step 5 And now write ‘localhost:8000’ in browser

simplicity-acquired-taste

Here, we have successfully created the laravel project and is ready to use.

 

Step 6 Create Controller in laravel project using following Command. look like this

demo-controller

Look Like This

php-demo-controller

 

Step 7 Now create a method in controller and write CURL to handle or use API.

Here we will be using CURL to initiate call to the API.
- CURL means Client URL.
- CURL is a background process.

  • In Laravel

Write following CURL code in a controller or route of your laravel project.


$username = $_POST['username'];
$password = $_POST['password'];
 
$session = curl_init();
 
$basecamp_url = 'https://basecamp.com/Your-Company-Id/api/v1/projects.json';
 
curl_setopt($session, CURLOPT_URL, $basecamp_url);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
 
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_USERAGENT, 'demo');
 
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
 
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
 
$response = curl_exec($session);
$data = json_decode($response);
curl_close($session);

Here, $response is the json data returned and $data is the json-to-array converted data.
- $username and $password are post data that we need to pass with the request.
- curl_init() is used to initiate the CURL request in php.
- $basecamp_url is used to store API link.
- curl_setopt() is used to set other setting options in curl.
- Many a times CURLOPT_SSL_VERIFYPEER option create a problem in local environment. So this option is always set to false when testing.
- curl_exec() is used to execute the curl request and return JSON data.
- json_decode is used to convert this JSON data into an array.
- curl_close() is used to close the cURL request.

 

Step 8 The API response is as under:

object-class

That was all from the basecamp APIs. You can try out other APIs from above link. I hope this blog helps in Using Basecamp APIs. Let me know in comment if you have any questions regarding Web. I will reply you ASAP.

Got an Idea of Web Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Web Development Company in India.

As a Web Designer I designed the most stunning websites. I like to write & share tips & tricks about website design techniques.

face mask Belial The Demon Headgear Pocket Staff Magic