Querying information from Harvest

The Harvest API library allows you full access to the client and project information you have logged in Harvest. In the below examples we will take a quick look at how to use the Harvest api PHP interface library to obtain a single client as well as all of your projects.

Obtaining a Single Client’s Information

To query Harvest for a client you will first need to know it’s client_id. You can find this any number of ways but the easiest for beginners to the Harvest API is to log in to their Harvest Account, access their client list and select the client they want to query via the API. Once they are on the Client’s edit page they can find the client-id in the url after /clients/. Once you have the ID you then would use the getClient method of the library. But lets not rush ahead of ourselves lets take things step by step.

  1. Configure the Library
    require_once(dirname(__FILE__) . '/HarvestAPI.php');
    
    /* Register Auto Loader */
    spl_autoload_register(array('HarvestAPI', 'autoload'));
    
    $api = new HarvestAPI();
    $api->setUser( "user@email.com" );
    $api->setPassword( "password" );
    $api->setAccount( "account" );
    	
    $api->setRetryMode( HarvestAPI::RETRY );
    $api->setSSL(true);
    

    * remember to set SSL support only if you have it enabled for your account.

  2. Query for the Client
    $result= $api->getClient( 123456 );
    
  3. Verify the query was successful
    if( $result->isSuccess() ) {
    
    }
    
  4. Utilize the returned client data
    $client = $result->data;
    
    echo $client->get( "name" );
    echo $client->details;
    

The complete getClient example:

require_once(dirname(__FILE__) . '/HarvestAPI.php');

/* Register Auto Loader */
spl_autoload_register(array('HarvestAPI', 'autoload'));

$api = new HarvestAPI();
$api->setUser( "user@email.com" );
$api->setPassword( "password" );
$api->setAccount( "account" );
	
$api->setRetryMode( HarvestAPI::RETRY );
$api->setSSL(true);

$result = $api->getClient( 123456 );
if( $result->isSuccess() ) {
    $client = $result->data;
    echo $client->get( "name" );
    echo $client->details;
}

Obtaining all of your Projects

To get the details on all the projects you currently have in Harvest then you can utilize the getProjects method of the library. The first step will again be configuring the library so lets jump right into the method call.

  1. call the getProjects method
    $result = $api->getProjects();
    
  2. verify the request was successful
    if( $result->isSuccess() ) {
    
    }
    
  3. Iterate over the returned projects
    foreach( $result->data as $project ) {
    
    }
    
  4. Output or use the project information
    echo $project->name;
    echo $project->billable;
    echo $project->active;
    

The complete getProjects example:

require_once(dirname(__FILE__) . '/HarvestAPI.php');

/* Register Auto Loader */
spl_autoload_register(array('HarvestAPI', 'autoload'));

$api = new HarvestAPI();
$api->setUser( "user@email.com" );
$api->setPassword( "password" );
$api->setAccount( "account" );
	
$api->setRetryMode( HarvestAPI::RETRY );
$api->setSSL(true);

$result = $api->getProjects( );
if( $result->isSuccess() ) {
    foreach( $result->data as $project ) {
        echo $project->get( "name" );
        echo $project->billable;
        echo $project->active;
    }
}

Comments & Questions

  1. First you wrote “echo $project->name;” and then “echo $project->get( “name” );”
    what’s right and what’s wrong???

    1. They both are correct. You can either get the Project Name directly via $project->name or you can use the get method $project->get(“name”). Either method will work it is user preference which one you would like to you use.

Add Your Comment