Re: Creating 'party' object in php

59 views
Skip to first unread message
Message has been deleted

Adam Wilk

unread,
Oct 29, 2018, 12:44:38 PM10/29/18
to Capsule API
Hi Will,

My PHP is rusty, but I think this should hopefully give you a working example...

<?php

// get cURL resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, 'https://api.capsulecrm.com/api/v2/parties');

// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Authorization: Bearer ***** Hidden credentials *****',
  'Content-Type: application/json',
]);

// json body
$json_array = [
  'party' => [
    'firstName' => 'Test',
    'emailAddresses' => [
      [
        'type' => 'work',
        'address' => 'wgs...@yahoo.com'
      ]
    ],
    'addresses' => [
      [
        'type' => null,
        'country' => 'United Kingdom'
      ]
    ],
    'jobTitle' => 'Director',
    'type' => 'person',
    'phoneNumbers' => [
      [
        'type' => null,
        'number' => '01234 567 890'
      ]
    ],
    'organisation' => [
      'name' => 'Acme Products'
    ],
    'lastName' => 'Tester'
  ]
]; 
$body = json_encode($json_array);

// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// send the request and save response to $response
$response = curl_exec($ch);

// stop if fails
if (!$response) {
  die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

// close curl resource to free up system resources 
curl_close($ch);

Cheers,
Adam

On Monday, October 29, 2018 at 4:20:15 PM UTC, Will Fairhurst wrote:
Hi,

I'm struggling to create properly formatted json to create a new party.  Our code results in a 400 Bad Request - Invalid JSON Format.

The code is as follows, any help greatly appreciated.

$party_info = array(
  'party' => array(
    'type'      =>  'person',
    'firstName' =>  'Test',
    'lastName' =>  'Tester',
    'jobTitle' =>  'Director',
    'organisation' =>  array(
      'name'  => 'Acme Products'
    ),
    'emailAddresses'  =>  array(
      'type'  =>  'work',
      'address'  =>  'wgs...@yahoo.com'
    ),
    'phoneNumbers'  =>  array(
      'type'  =>  null,
      'number'  =>  '01234 567 890'
    ),
    'addresses'  =>  array(
      'type'  =>  null,
      'country'  =>  'United Kingdom'
    ),
  )
);

$party_info_json = json_encode( $party_info );
$post_response = $client->request('POST',$capsule_api_url.'parties/', [
    'headers' => [
        'Authorization' => "Bearer $capsule_personal_access_token",
        //'Accept' => 'application/json',
        'Content-Type' => "application/json",
    ],
    'CURLOPT_POSTFIELDS'  => $party_info_json
]);

Cheers,

Will

Will Fairhurst

unread,
Oct 29, 2018, 2:07:28 PM10/29/18
to Capsule API
Adam,

Thanks for such a quick reply and for providing a working solution!  Massive help, thanks again.

Will
Reply all
Reply to author
Forward
0 new messages