Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created June 17, 2015 22:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iaintshine/ae3ba8f997259ccecdbb to your computer and use it in GitHub Desktop.
Save iaintshine/ae3ba8f997259ccecdbb to your computer and use it in GitHub Desktop.
Create a new lead using php 5.3 and Base API v2
<?php
function createLead($accessToken, array $lead)
{
$method = 'post';
$absUrl = 'https://api.getbase.com/v2/leads';
$headers = array(
'User-Agent: BaseCRM/PHP Sample',
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json',
);
$envelope = array('data' => $lead);
$payload = json_encode($envelope);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $absUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
$resp = curl_exec($curl);
if ($resp === false)
{
$errno = curl_errno($curl);
$error_message = curl_error($curl);
curl_close($curl);
throw new Exception($error_message);
}
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$newLead = null;
if ($resp != null && $code == 200)
{
try
{
$response = json_decode($resp, true);
}
catch (Exception $e)
{
$msg = "Unknown error occurred. The response should be a json response. "
. "HTTP response code={$code}. "
. "HTTP response body={$rawResponse}.";
throw new Exception($msg);
}
$newLead = $response['data'];
}
return $newLead;
}
function getAccessToken()
{
$token = getenv("BASECRM_ACCESS_TOKEN");
if (!$token) throw new Exception('"BASECRM_ACCESS_TOKEN" environment variable has not been found.');
return $token;
}
$lead = array(
'first_name' => 'John',
'last_name' => 'Doe'
);
print_r(createLead(getAccessToken(), $lead));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment