An Easy PHP Function To Run Your Harvest API Calls Through

So, you need to make requests to and from the Harvest API in your PHP website or application. Here is a relatively straight forward PHP function you can use to take care of all the leg work in making JSON requests to the Harvest API.

You can either run this all in the one page, or better yet, place the below in an include file, out of sight. All you would need to customise is the $username and $password variables based on your Harvest account and you are good to go.

<?php

// here we set our default settings like username and password
$username = "you@youremail.com"; // your Harvest email address
$password = "password"; // your password
$encodedcredentials = base64_encode($username . ":" . $password);

// this function is used to return the headers from the api response
function get_headers_from_curl_response($response) {
   $headers = array();
   $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
   foreach (explode("\r\n", $header_text) as $i => $line)
     if ($i === 0)
       $headers['http_code'] = $line;
     else
     {
     list ($key, $value) = explode(': ', $line);
     $headers[$key] = $value;
   }
   return $headers;
}

// our main function to send api requests
function harvest_curl($url, $json, $encodedcredentials, $type) {

   $curl = curl_init();

   curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_HEADER => true,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => $type,
      CURLOPT_POSTFIELDS => $json,
      CURLOPT_HTTPHEADER => array(
         "accept: application/json",
         "authorization: Basic " . $encodedcredentials . "",
         "cache-control: no-cache",
         "content-type: application/json"
         ),
    ));

   $response = curl_exec($curl);
   $err = curl_error($curl);
 
   curl_close($curl);

   if ($err) {
      echo "cURL Error #:" . $err;
   } else {
      return $response;
   }
}

Using the Function

Using the function is also pretty straight forward and I have even included below an example of inserting a project with only the bare basics of functionality required to get you started.

// set the Harvest API location
$addproject_url = "https://yourcompanyname.harvestapp.com/projects";

// define your actual JSON request to add a project
$addproject_json = '{
   "project": {
     "client_id": 123456,
     "name": "Project Name",
     "active": true
   }
}';

// run the function
$addproject = harvest_curl($addproject_url, $addproject_json, $encodedcredentials, "POST");

// show the output
echo $addproject;

The main things you would need to customise to get the above working is firstly the api url, which is custom url you use to access Harvest through, and the the actual JSON request, which in this example of adding a project you would need to enter a valid client_id, which you can find by going to the edit client page in Harvest and the ID is shown in the url.

Interpreting Headers

A lot of the responses from the Harvest API, such as if the request has been successful or not, are returned in the headers. We also defined in our include a function to easily translate these headers into a usable array. Below is an example of how this can be used for error handling.

$headers = get_headers_from_curl_response($addproject);

if ($headers['Status'] == "201 Created") {
   echo "Success "
} else {
   echo "Error: " . $addproject . '<br>';
}

API Reference

The Harvest API is very well documented and you can find many examples of JSON requests at the link below.

https://help.getharvest.com/api-v1/

 

Leave a Reply

Your email address will not be published. Required fields are marked *