Updating contacts using PUT (PHP CURL)

Hello,

We are planning on using the FreeAgent API to allow our customers to check
and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding it
impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My
Organisation",“first_name”:“Joe”,“last_name”:“Bloggs”,“email”:"joe@bloggs.com”,“phone_number”:“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim

Tim,

Just in case it helps you to know, I was able to update a task by only
sending the changed data, so I didn’t need to send every key->value pair
for it to be accepted. The changes reflected in the UI instantly too
(though I am working in the sandbox)

Dave.On Sunday, 23 March 2014 11:18:47 UTC+1, Tim Ballard wrote:

Hello,

We are planning on using the FreeAgent API to allow our customers to check
and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding it
impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My
Organisation”,“first_name”:“Joe”,“last_name”:“Bloggs”,“email”:"
j...@bloggs.com <javascript:>",“phone_number”:“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim

This is how I do it…

<?
$accesstoken = file_get_contents($tokenFile);
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$accesstoken;
function dbg_curl_data($curl, $data=null) {
  static $buffer = '';
  if ( is_null($curl) ) {
    $r = $buffer;
    $buffer = '';
    return $r;
  }
  else {
    $buffer .= $data;
    return strlen($data);
  }
}
if($_POST['action'] == "update_contact") {
 //Validation and Sanitisation of user input is here
 //Find out this contact's FreeAgent contact ID
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
 curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => 'https://api.freeagent.com/v2/contacts?per_page=100',
  CURLOPT_USERAGENT => 'YOUR ORGANISATION NAME'
 ));
 $response = curl_exec($ch);
 curl_close($ch); 
 
 $decoded = json_decode($response, true);
 
 //Let's assume we now we have the key / contact ID of the contact that we 
are looking to update
 $key = recursive_array_search($CONTACT_NAME, $decoded['contacts']);   
 
 if(!empty($_POST['first_name'])){  $params['contact']['first_name']   = 
$_POST['first_name'];}
 if(!empty($_POST['last_name'])){  $params['contact']['last_name']   = 
$_POST['last_name'];}
 if(!empty($_POST['email'])){   $params['contact']['email']    = 
$_POST['email'];}
 if(!empty($_POST['phone_number'])){  $params['contact']['phone_number'] 
  = $_POST['phone_number'];}
 if(!empty($_POST['address1'])){   $params['contact']['address1']    = 
$_POST['address1'];}
 if(!empty($_POST['town'])){    $params['contact']['town']     = 
$_POST['town'];}
 if(!empty($_POST['region'])){   $params['contact']['region']    = 
$_POST['region'];}
 if(!empty($_POST['postcode'])){   $params['contact']['postcode']    = 
$_POST['postcode'];}
 if(!empty($_POST['country'])){   $params['contact']['country']    = 
$_POST['country'];}
 
 $ch2 = curl_init();
 $headr2[] = 'Accept: application/json';
 $headr2[] = 'Content-type: application/json';
 $headr2[] = 'Authorization: OAuth '.$accesstoken;
 curl_setopt($ch2, CURLOPT_HEADERFUNCTION, 'dbg_curl_data');
 curl_setopt($ch2, CURLOPT_WRITEFUNCTION, 'dbg_curl_data');
 curl_setopt($ch2, CURLINFO_HEADER_OUT, true);
 curl_setopt($ch2, CURLOPT_HTTPHEADER,$headr2);
 curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "PUT");
 curl_setopt($ch2, CURLOPT_POSTFIELDS,json_encode($params));
 curl_setopt_array($ch2, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => $decoded['contacts'][$key]['url'],
  CURLOPT_USERAGENT => 'YOUR ORGANISATION NAME'
 ));
 $response = curl_exec($ch2);
  
    //print_r(curl_getinfo($ch2));
 $decoded = json_decode($response, true);
}
?>

Hope that helps.On Tuesday, 8 July 2014 23:04:23 UTC+1, Daniel Bird wrote:

Hi Tim,
Were you able to document in detail somewhere how you got
Update to work with PUT / PHP / CURL?

I am able to create a contact, able to retrieve all contacts, able to
retrieve a single contact, just unable to update or delete a contact.

Any help would be much appreciated.

On Monday, 24 March 2014 10:24:34 UTC+13, Tim Ballard wrote:

Hi Dave,

Thank you for the clues.

I have now managed to update perform a few successful updates on our live
Free Agent account using just the standard PHP curl functions (no extra
OAuth library needed it seems). I think the problem may have been down to
me trying to specify the content-length before actually JSON encoding the
array when in fact I did not need to specify content-length at all.

Like you said, the “contact” wrapper was required and I only needed to
specify the fields that needed to be updated.

The working code is actually quite simple if I remove all my experiments
that are now //comments. It would be good to have just a few PHP examples
in the documentation to save other people’s time and sanity.

Kind regards,
Tim

On Sunday, 23 March 2014 19:45:00 UTC, David Goodchild wrote:

Tim,

Just in case it helps you to know, I was able to update a task by only
sending the changed data, so I didn’t need to send every key->value pair
for it to be accepted. The changes reflected in the UI instantly too
(though I am working in the sandbox)

Dave.

On Sunday, 23 March 2014 11:18:47 UTC+1, Tim Ballard wrote:

Hello,

We are planning on using the FreeAgent API to allow our customers to
check and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding
it impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My
Organisation”,“first_name”:“Joe”,“last_name”:“Bloggs”,“email”:"
j...@bloggs.com",“phone_number”:“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim

Hi Tim,

I too am implementing in PHP. If it helps to know, I’m using the oAuth
library by “adoy” (installed via composer).

I now know, with high confidence you will need to have something like*
json_encode( array( ‘contact’ => array( /your key value pairs/ ) ).*

I don’t yet know about needing all the info, but for the sake of testing, I
would request the contact, edit the values you want changed and try sending
the whole lot again.

I’ll be testing more of the API soon, so will let you know if I discover
anything that could help you,

Cheers,
Dave.On Sunday, 23 March 2014 11:18:47 UTC+1, Tim Ballard wrote:

Hello,

We are planning on using the FreeAgent API to allow our customers to check
and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding it
impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My
Organisation”,“first_name”:“Joe”,“last_name”:“Bloggs”,“email”:"
j...@bloggs.com <javascript:>",“phone_number”:“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim

Hi Dave,

Thank you for the clues.

I have now managed to update perform a few successful updates on our live
Free Agent account using just the standard PHP curl functions (no extra
OAuth library needed it seems). I think the problem may have been down to
me trying to specify the content-length before actually JSON encoding the
array when in fact I did not need to specify content-length at all.

Like you said, the “contact” wrapper was required and I only needed to
specify the fields that needed to be updated.

The working code is actually quite simple if I remove all my experiments
that are now //comments. It would be good to have just a few PHP examples
in the documentation to save other people’s time and sanity.

Kind regards,
TimOn Sunday, 23 March 2014 19:45:00 UTC, David Goodchild wrote:

Tim,

Just in case it helps you to know, I was able to update a task by only
sending the changed data, so I didn’t need to send every key->value pair
for it to be accepted. The changes reflected in the UI instantly too
(though I am working in the sandbox)

Dave.

On Sunday, 23 March 2014 11:18:47 UTC+1, Tim Ballard wrote:

Hello,

We are planning on using the FreeAgent API to allow our customers to
check and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding it
impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My
Organisation”,“first_name”:“Joe”,“last_name”:“Bloggs”,“email”:"
j...@bloggs.com",“phone_number”:“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim

Sweet, got it all working. Thank you for your help. Good karma for you!

Kind regards,

Daniel Bird
danieldbird@gmail.comOn 10 July 2014 01:00, Tim Ballard tim@tim-ballard.co.uk wrote:

This is how I do it…

<?
> $accesstoken = file_get_contents($tokenFile);
> $headr[] = 'Content-length: 0';
> $headr[] = 'Content-type: application/json';
> $headr[] = 'Authorization: OAuth '.$accesstoken;
> function dbg_curl_data($curl, $data=null) {
>   static $buffer = '';
>   if ( is_null($curl) ) {
>     $r = $buffer;
>     $buffer = '';
>     return $r;
>   }
>   else {
>     $buffer .= $data;
>     return strlen($data);
>   }
> }
> if($_POST['action'] == "update_contact") {
>  //Validation and Sanitisation of user input is here
>  //Find out this contact's FreeAgent contact ID
>  $ch = curl_init();
>  curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
>  curl_setopt_array($ch, array(
>   CURLOPT_RETURNTRANSFER => 1,
>   CURLOPT_URL => 'https://api.freeagent.com/v2/contacts?per_page=100',
>   CURLOPT_USERAGENT => 'YOUR ORGANISATION NAME'
>  ));
>  $response = curl_exec($ch);
>  curl_close($ch);
>
>  $decoded = json_decode($response, true);
>
>  //Let's assume we now we have the key / contact ID of the contact that we
> are looking to update
>  $key = recursive_array_search($CONTACT_NAME, $decoded['contacts']);
>
>  if(!empty($_POST['first_name'])){  $params['contact']['first_name']   =
> $_POST['first_name'];}
>  if(!empty($_POST['last_name'])){  $params['contact']['last_name']   =
> $_POST['last_name'];}
>  if(!empty($_POST['email'])){   $params['contact']['email']    =
> $_POST['email'];}
>  if(!empty($_POST['phone_number'])){  $params['contact']['phone_number']
>   = $_POST['phone_number'];}
>  if(!empty($_POST['address1'])){   $params['contact']['address1']    =
> $_POST['address1'];}
>  if(!empty($_POST['town'])){    $params['contact']['town']     =
> $_POST['town'];}
>  if(!empty($_POST['region'])){   $params['contact']['region']    =
> $_POST['region'];}
>  if(!empty($_POST['postcode'])){   $params['contact']['postcode']    =
> $_POST['postcode'];}
>  if(!empty($_POST['country'])){   $params['contact']['country']    =
> $_POST['country'];}
>
>  $ch2 = curl_init();
>  $headr2[] = 'Accept: application/json';
>  $headr2[] = 'Content-type: application/json';
>  $headr2[] = 'Authorization: OAuth '.$accesstoken;
>  curl_setopt($ch2, CURLOPT_HEADERFUNCTION, 'dbg_curl_data');
>  curl_setopt($ch2, CURLOPT_WRITEFUNCTION, 'dbg_curl_data');
>  curl_setopt($ch2, CURLINFO_HEADER_OUT, true);
>  curl_setopt($ch2, CURLOPT_HTTPHEADER,$headr2);
>  curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "PUT");
>  curl_setopt($ch2, CURLOPT_POSTFIELDS,json_encode($params));
>  curl_setopt_array($ch2, array(
>   CURLOPT_RETURNTRANSFER => 1,
>   CURLOPT_URL => $decoded['contacts'][$key]['url'],
>   CURLOPT_USERAGENT => 'YOUR ORGANISATION NAME'
>  ));
>  $response = curl_exec($ch2);
>
>     //print_r(curl_getinfo($ch2));
>  $decoded = json_decode($response, true);
> }
> ?>

Hope that helps.

On Tuesday, 8 July 2014 23:04:23 UTC+1, Daniel Bird wrote:

Hi Tim,
Were you able to document in detail somewhere how you got
Update to work with PUT / PHP / CURL?

I am able to create a contact, able to retrieve all contacts, able to
retrieve a single contact, just unable to update or delete a contact.

Any help would be much appreciated.

On Monday, 24 March 2014 10:24:34 UTC+13, Tim Ballard wrote:

Hi Dave,

Thank you for the clues.

I have now managed to update perform a few successful
updates on our live Free Agent account using just the standard PHP curl
functions (no extra OAuth library needed it seems). I think the problem
may have been down to me trying to specify the content-length before
actually JSON encoding the array when in fact I did not need to
specify content-length at all.

Like you said, the “contact” wrapper was required and I only needed to
specify the fields that needed to be updated.

The working code is actually quite simple if I remove all my experiments
that are now //comments. It would be good to have just a few PHP examples
in the documentation to save other people’s time and sanity.

Kind regards,
Tim

On Sunday, 23 March 2014 19:45:00 UTC, David Goodchild wrote:

Tim,

Just in case it helps you to know, I was able to update a task by only
sending the changed data, so I didn’t need to send every key->value pair
for it to be accepted. The changes reflected in the UI instantly too
(though I am working in the sandbox)

Dave.

On Sunday, 23 March 2014 11:18:47 UTC+1, Tim Ballard wrote:

Hello,

We are planning on using the FreeAgent API to allow our customers to
check and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding
it impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My Organisation”,“first_name”:"
Joe",“last_name”:“Bloggs”,“email”:"j...@bloggs.com","
phone_number":“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim


You received this message because you are subscribed to a topic in the
Google Groups “FreeAgent API” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/freeagent_api/9p1ctoYudqo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
freeagent_api+unsubscribe@googlegroups.com.
To post to this group, send email to freeagent_api@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/d/optout.

Hi Tim,
Were you able to document in detail somewhere how you got Update
to work with PUT / PHP / CURL?

I am able to create a contact, able to retrieve all contacts, able to
retrieve a single contact, just unable to update or delete a contact.

Any help would be much appreciated.On Monday, 24 March 2014 10:24:34 UTC+13, Tim Ballard wrote:

Hi Dave,

Thank you for the clues.

I have now managed to update perform a few successful updates on our live
Free Agent account using just the standard PHP curl functions (no extra
OAuth library needed it seems). I think the problem may have been down to
me trying to specify the content-length before actually JSON encoding the
array when in fact I did not need to specify content-length at all.

Like you said, the “contact” wrapper was required and I only needed to
specify the fields that needed to be updated.

The working code is actually quite simple if I remove all my experiments
that are now //comments. It would be good to have just a few PHP examples
in the documentation to save other people’s time and sanity.

Kind regards,
Tim

On Sunday, 23 March 2014 19:45:00 UTC, David Goodchild wrote:

Tim,

Just in case it helps you to know, I was able to update a task by only
sending the changed data, so I didn’t need to send every key->value pair
for it to be accepted. The changes reflected in the UI instantly too
(though I am working in the sandbox)

Dave.

On Sunday, 23 March 2014 11:18:47 UTC+1, Tim Ballard wrote:

Hello,

We are planning on using the FreeAgent API to allow our customers to
check and update their billing details through our website when signed in.

So far, I have been able to read contact information but I am finding it
impossible to update existing contact information. I seem to be able to
submit an update request without receiving an error but nothing gets
updated.

Here are my request headers, any ideas why this is not working?

PUT /v2/contacts/597884 HTTP/1.1
User-Agent: Lawford Education Ltd
Host: api.freeagent.com
Accept: application/json
Content-type: application/json
Authorization: OAuth MY_TOKEN

{“organisation_name”:“My
Organisation”,“first_name”:“Joe”,“last_name”:“Bloggs”,“email”:"
j...@bloggs.com",“phone_number”:“01234567890”,“address1”:“The
House”,“town”:“Manningtree”,“region”:“Essex”,“postcode”:“AB12CD”,“country”:“United
Kingdom”}

I have tried wrapping the array in a “contact” tag as well but still
nothing. Do I need to include all the input parameters, or just those that
need to be updated?

Many thanks in advance,
Tim