Created
November 4, 2015 09:44
-
-
Save TiagoGouvea/fcee96d107e8a8e7b816 to your computer and use it in GitHub Desktop.
AgileCRM PHP Api Wrap with some fixes
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 | |
define("AGILE_DOMAIN", "tiagogouvea"); | |
define("AGILE_USER_EMAIL", "yourEmail"); | |
define("AGILE_REST_API_KEY", "yourApiKey"); | |
function curl_wrap($entity, $data, $method) | |
{ | |
$agile_url = "https://" . AGILE_DOMAIN . ".agilecrm.com/dev/api/" . $entity; | |
$agile_php_url = "https://" . AGILE_DOMAIN . ".agilecrm.com/core/php/api/" . $entity . "?id=" . AGILE_REST_API_KEY; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); | |
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true); | |
switch ($method) { | |
case "POST": | |
$url = ($entity == "tags" ? $agile_php_url : $agile_url); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
// echo "<b>Post:</b><br>"; | |
// echo $url . '<br>'; | |
// var_dump($data); | |
break; | |
case "GET": | |
$url = ($entity == "tags" ? $agile_php_url . '&email=' . $data->{'email'} : $agile_url); | |
// var_dump($url); | |
// die(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
break; | |
case "PUT": | |
$url = ($entity == "tags" ? $agile_php_url : $agile_url); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
break; | |
case "DELETE": | |
$url = ($entity == "tags" ? $agile_php_url : $agile_url); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
break; | |
default: | |
break; | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-type : application/json; charset : UTF-8;', | |
'Accept : application/json' | |
)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERPWD, AGILE_USER_EMAIL . ':' . AGILE_REST_API_KEY); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 120); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$output = curl_exec($ch); | |
// Check if any error occured | |
if (curl_errno($ch)) { | |
var_dump(curl_errno($ch)); | |
var_dump(curl_error($ch)); | |
die("curl_Error"); | |
} | |
curl_close($ch); | |
return $output; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment