Searching Contacts By Email

Hi Neil,

At the moment that is the only way to find contacts by email via the API.

Regarding getting a subset of contacts, you are getting that because we
paginate the response. To get the other pages, you can look at the “Link”
header on the response and grab the URLs from there:

Link: https://api.freeagent.com/v2/invoices?page=4&per_page=50; rel=“prev”, https://api.freeagent.com/v2/invoices?page=6&per_page=50; rel=“next”, https://api.freeagent.com/v2/invoices?page=1&per_page=50; rel=“first”, https://api.freeagent.com/v2/invoices?page=10&per_page=50; rel=“last”

You can figure out when you’re at the last page by looking at this header
and checking for the “next” or “last” links – if they’re not there, there
are no more pages you can request.

You can also request up to 100 contacts per page in order to minimise the
number of requests needed.

Regards,
IoanOn Tuesday, 26 March 2013 11:35:34 UTC, Neil Matthews wrote:

I’m trying to do the following:

  1. Search all contacts and pull them back into an object
  2. Search that object for contact matching an email address
  3. If I get a match pull back the contact URL so I can add new invoices
    against that contact
  4. else add new contact and add new invoice to that

I’m using the following code for step 1 and it works fine to a point, but
when I’ve added a few hundred contacts to freeagent the object does not
return all contacts and I start creating new contacts and assigning them
invoices.

The same thing happens in the google playground only a sub set of contacts
is returned,

This is pretty inefficient way to query contacts, is there a better way?

Thanks

Neil Matthews

function freeagent_lookup_client( $accessToken){

$c = curl_init(‘https://api.freeagent.com/v2/contacts’);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
'Authorization: Bearer '.$accessToken
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_GET, true);
curl_setopt($c, CURLOPT_USERAGENT, ‘SEO Training cart66 integration’);

$result = print_r(curl_exec($c), true);
return $result;

}

Hi Neil,

You need to set the CURLOPT_HEADER option to true to get the headers back
with the output. Once you have it, you can search for the links in
whichever way you want (here I used a very simple regular expression
looking for the ‘next’ link):

<?php > $contacts = curl_get('https://api.freeagent.com/v2/contacts'); > printf('Got all contacts: '.$contacts); // Do some work with the full > result, probably want to strip out the headers first. > function curl_get($url) { > $curl = curl_init($url); > curl_setopt_array($curl, array( > CURLOPT_HEADER => true, > CURLOPT_HTTPHEADER => array('Accept: application/json', > 'Authorization: Bearer '.$accessToken), > CURLOPT_RETURNTRANSFER => true, > CURLOPT_USERAGENT => 'test' > )); > $result = curl_exec($curl); > curl_close($curl); $full_output = $result; > $matches = array(); > preg_match("/<(.+)>; rel='next'/", $result, $matches); // Also > probably want something a bit more sophisticated here > $next_page = $matches[1]; > if ($next_page) { > $full_output .= curl_get($next_page); > } > return $full_output; > } > ?>

Looking for the previous page is as simple as looking for the links with
rel=‘prev’. Also, when you’re on the first page you’ll have no ‘prev’ link
and when you’re on the last page there’s no ‘last’ link.

Hope it helps,
IoanOn Tuesday, 9 April 2013 10:45:55 UTC+1, Neil Matthews wrote:

Hi Ioan

Have you got any php examples of paging through the response? That’s not
something I’ve ever done before

Thanks

Neil

On Wednesday, March 27, 2013 2:35:44 PM UTC, io...@freeagent.com wrote:

Hi Neil,

At the moment that is the only way to find contacts by email via the API.

Regarding getting a subset of contacts, you are getting that because we
paginate the response. To get the other pages, you can look at the “Link”
header on the response and grab the URLs from there:

Link: https://api.freeagent.com/v2/invoices?page=4&per_page=50; rel=“prev”, https://api.freeagent.com/v2/invoices?page=6&per_page=50; rel=“next”, https://api.freeagent.com/v2/invoices?page=1&per_page=50; rel=“first”, https://api.freeagent.com/v2/invoices?page=10&per_page=50; rel=“last”

You can figure out when you’re at the last page by looking at this header
and checking for the “next” or “last” links – if they’re not there, there
are no more pages you can request.

You can also request up to 100 contacts per page in order to minimise the
number of requests needed.

Regards,
Ioan

On Tuesday, 26 March 2013 11:35:34 UTC, Neil Matthews wrote:

I’m trying to do the following:

  1. Search all contacts and pull them back into an object
  2. Search that object for contact matching an email address
  3. If I get a match pull back the contact URL so I can add new invoices
    against that contact
  4. else add new contact and add new invoice to that

I’m using the following code for step 1 and it works fine to a point,
but when I’ve added a few hundred contacts to freeagent the object does not
return all contacts and I start creating new contacts and assigning them
invoices.

The same thing happens in the google playground only a sub set of
contacts is returned,

This is pretty inefficient way to query contacts, is there a better way?

Thanks

Neil Matthews

function freeagent_lookup_client( $accessToken){

$c = curl_init(‘https://api.freeagent.com/v2/contacts’);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
'Authorization: Bearer '.$accessToken
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_GET, true);
curl_setopt($c, CURLOPT_USERAGENT, ‘SEO Training cart66 integration’);

$result = print_r(curl_exec($c), true);
return $result;

}

Hi Ioan

Have you got any php examples of paging through the response? That’s not
something I’ve ever done before

Thanks

NeilOn Wednesday, March 27, 2013 2:35:44 PM UTC, io...@freeagent.com wrote:

Hi Neil,

At the moment that is the only way to find contacts by email via the API.

Regarding getting a subset of contacts, you are getting that because we
paginate the response. To get the other pages, you can look at the “Link”
header on the response and grab the URLs from there:

Link: https://api.freeagent.com/v2/invoices?page=4&per_page=50; rel=“prev”, https://api.freeagent.com/v2/invoices?page=6&per_page=50; rel=“next”, https://api.freeagent.com/v2/invoices?page=1&per_page=50; rel=“first”, https://api.freeagent.com/v2/invoices?page=10&per_page=50; rel=“last”

You can figure out when you’re at the last page by looking at this header
and checking for the “next” or “last” links – if they’re not there, there
are no more pages you can request.

You can also request up to 100 contacts per page in order to minimise the
number of requests needed.

Regards,
Ioan

On Tuesday, 26 March 2013 11:35:34 UTC, Neil Matthews wrote:

I’m trying to do the following:

  1. Search all contacts and pull them back into an object
  2. Search that object for contact matching an email address
  3. If I get a match pull back the contact URL so I can add new invoices
    against that contact
  4. else add new contact and add new invoice to that

I’m using the following code for step 1 and it works fine to a point, but
when I’ve added a few hundred contacts to freeagent the object does not
return all contacts and I start creating new contacts and assigning them
invoices.

The same thing happens in the google playground only a sub set of
contacts is returned,

This is pretty inefficient way to query contacts, is there a better way?

Thanks

Neil Matthews

function freeagent_lookup_client( $accessToken){

$c = curl_init(‘https://api.freeagent.com/v2/contacts’);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
'Authorization: Bearer '.$accessToken
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_GET, true);
curl_setopt($c, CURLOPT_USERAGENT, ‘SEO Training cart66 integration’);

$result = print_r(curl_exec($c), true);
return $result;

}