-
-
Save humbletim/74979fa39f9e550836329844b71e675f to your computer and use it in GitHub Desktop.
Create a new lead using php 5.3 and Base API v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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