Created
April 20, 2016 09:52
-
-
Save christophrumpel/bccfd316ae0c4b08563fccb8f7122000 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| namespace Neos\Crm\Api; | |
| // CRM credentials | |
| define('CLIENT_KEY', ''); | |
| define('CLIENT_SECRET', ''); | |
| define('API_KEY', ''); | |
| /* | |
| * This file is a demo integration of neos esb/crm api | |
| * | |
| * (c)2014 | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| /** | |
| * Class Client | |
| * | |
| * @package Neos\Crm\Api | |
| * @author Ludwig Ruderstaller <[email protected]> | |
| */ | |
| class Client | |
| { | |
| /** | |
| * @var string|null | |
| */ | |
| protected $apiKey = null; | |
| /** | |
| * @var string|null | |
| */ | |
| protected $apiSec = null; | |
| /** | |
| * @var string | |
| */ | |
| protected $apiUrl = 'https://crm.neos.eu/api/v1/'; | |
| /** | |
| * @var string | |
| */ | |
| protected $tokenUrl = 'https://crm.neos.eu/oauth/v2/token'; | |
| /** | |
| * @var string | |
| */ | |
| protected $grantType = 'http://crm.neos.eu/grants/api_key'; | |
| /** | |
| * @var string | |
| */ | |
| protected $format = 'json'; | |
| public function __construct($apiKey, $apiSec) | |
| { | |
| $this->apiKey = $apiKey; | |
| $this->apiSec = $apiSec; | |
| if (isset($_SESSION['token_expire']) && date("U") > $_SESSION['token_expire']) { | |
| $this->refreshToken(); | |
| } | |
| } | |
| /** | |
| * | |
| * @return mixed | |
| * @throws \Exception | |
| */ | |
| public function getCustomFields() | |
| { | |
| return $this->call('fields'); | |
| } | |
| /** | |
| * Add contact | |
| * | |
| * @param $data | |
| * | |
| * @return mixed | |
| * @throws \Exception | |
| */ | |
| public function addContact($data) | |
| { | |
| return $this->call('contacts/', $data, 'POST'); | |
| } | |
| /** | |
| * @return bool|mixed | |
| * @throws \Exception | |
| */ | |
| public function refreshToken() | |
| { | |
| if (!isset($_SESSION['refresh_token'])) { | |
| return false; | |
| } | |
| return $this->getToken(null, null, true); | |
| } | |
| /** | |
| * @param string $apiKey | |
| * | |
| * @return mixed | |
| * @throws \Exception | |
| */ | |
| public function login($apiKey) | |
| { | |
| return $this->getToken($apiKey); | |
| } | |
| /** | |
| * Check if token is valid, if refresh token is present useit. | |
| * @return bool | |
| */ | |
| public function isTokenValid() | |
| { | |
| if (isset($_SESSION['refresh_token']) && isset($_SESSION['token_expire']) && date("U") > $_SESSION['token_expire']) { | |
| $this->refreshToken(); | |
| return true; | |
| } elseif (isset($_SESSION['refresh_token']) && isset($_SESSION['token_expire']) && date("U") < $_SESSION['token_expire']) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| /** | |
| * @param null|string $apiKey | |
| * @param bool $refreshToken | |
| * | |
| * @return mixed | |
| * @throws \Exception | |
| */ | |
| protected function getToken($apiKey = null, $refreshToken = false) | |
| { | |
| $options = array( | |
| 'client_id' => $this->apiKey, | |
| 'client_secret' => $this->apiSec, | |
| ); | |
| if ($refreshToken && isset($_SESSION['refresh_token'])) { | |
| $options += array( | |
| 'grant_type' => 'refresh_token', | |
| 'refresh_token' => $_SESSION['refresh_token'] | |
| ); | |
| } else { | |
| $options += array( | |
| 'grant_type' => $this->grantType, | |
| 'api_key' => $apiKey | |
| ); | |
| } | |
| $result = $this->call('token', $options, 'GET', $this->tokenUrl); | |
| if (isset($result->error_description)) { | |
| throw new \Exception($result->error_description); | |
| } | |
| if (!isset($result->access_token)) { | |
| throw new \Exception('Access token not found, but no error given'); | |
| } | |
| $_SESSION['token'] = $result->access_token; | |
| $_SESSION['token_expire'] = date('U')+$result->expires_in; | |
| $_SESSION['refresh_token'] = $result->refresh_token; | |
| return $result; | |
| } | |
| /** | |
| * @param string $url | |
| * @param array $options | |
| * @param string $method | |
| * @param null $fullUrl | |
| * | |
| * @return mixed | |
| * @throws \Exception | |
| */ | |
| protected function call($url, $options = array(), $method = 'GET', $fullUrl = null) | |
| { | |
| $this->count = null; | |
| $kurl = $this->apiUrl.$url.'?access_token='.$_SESSION['token'].'&_format='.$this->format; | |
| if ($fullUrl !== null) { | |
| $kurl = $fullUrl.'?1=1'; | |
| } else { | |
| if (!isset($_SESSION['token'])) { | |
| throw new \Exception('No access token set, did you forget to login?'); | |
| } | |
| } | |
| $option = array( | |
| CURLOPT_FOLLOWLOCATION => 1, | |
| CURLOPT_RETURNTRANSFER => 1, | |
| CURLOPT_HTTPHEADER => array( | |
| 'Accept: application/json', | |
| 'Content-Type: application/json' | |
| ), | |
| ); | |
| if ($method == 'POST' || $method == 'PUT') { | |
| $option[CURLOPT_POST] = 1; | |
| $option[CURLOPT_POSTFIELDS] = json_encode($options); | |
| $option[CURLOPT_URL] = $kurl; | |
| } elseif ($method == 'GET') { | |
| $option[CURLOPT_URL] = $kurl.'&'.http_build_query($options); | |
| } | |
| if ($method == 'PUT') { | |
| unset($option[CURLOPT_POST]); | |
| $option[CURLOPT_CUSTOMREQUEST] = "PUT"; | |
| } | |
| //print_r($option); | |
| //print_r($options); | |
| $con = curl_init(); | |
| curl_setopt_array($con, $option); | |
| if (!($result = $this->curl_exec_follow($con)) && $method != 'PUT') { | |
| throw new \Exception('CURL Error: '.curl_error($con)); | |
| } | |
| curl_close($con); | |
| $result = json_decode($result); | |
| if (isset($result->error)) { | |
| throw new \Exception('ERROR: '.$result->error->message); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * @param string $apiURL | |
| * @return this | |
| */ | |
| public function setApiUrl($apiURL) | |
| { | |
| $this->apiUrl = $apiURL; | |
| return $this; | |
| } | |
| protected function curl_exec_follow($ch, &$maxredirect = null) { | |
| // we emulate a browser here since some websites detect | |
| // us as a bot and don't let us do our job | |
| $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)". | |
| " Gecko/20041107 Firefox/1.0"; | |
| curl_setopt($ch, CURLOPT_USERAGENT, $user_agent ); | |
| $mr = $maxredirect === null ? 5 : intval($maxredirect); | |
| if (filter_var(ini_get('open_basedir'), FILTER_VALIDATE_BOOLEAN) === false | |
| && filter_var(ini_get('safe_mode'), FILTER_VALIDATE_BOOLEAN) === false | |
| ) { | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); | |
| curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
| } else { | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); | |
| if ($mr > 0) | |
| { | |
| $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); | |
| $newurl = $original_url; | |
| $rch = curl_copy_handle($ch); | |
| curl_setopt($rch, CURLOPT_HEADER, true); | |
| curl_setopt($rch, CURLOPT_NOBODY, true); | |
| curl_setopt($rch, CURLOPT_FORBID_REUSE, false); | |
| do | |
| { | |
| curl_setopt($rch, CURLOPT_URL, $newurl); | |
| $header = curl_exec($rch); | |
| if (curl_errno($rch)) { | |
| $code = 0; | |
| } else { | |
| $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); | |
| if ($code == 301 || $code == 302) { | |
| preg_match('/Location:(.*?)\n/i', $header, $matches); | |
| $newurl = trim(array_pop($matches)); | |
| // if no scheme is present then the new url is a | |
| // relative path and thus needs some extra care | |
| if(!preg_match("/^https?:/i", $newurl)){ | |
| $newurl = $original_url . $newurl; | |
| } | |
| } else { | |
| $code = 0; | |
| } | |
| } | |
| } while ($code && --$mr); | |
| curl_close($rch); | |
| if (!$mr) | |
| { | |
| if ($maxredirect === null) | |
| trigger_error('Too many redirects.', E_USER_WARNING); | |
| else | |
| $maxredirect = 0; | |
| return false; | |
| } | |
| curl_setopt($ch, CURLOPT_URL, $newurl); | |
| } | |
| } | |
| return curl_exec($ch); | |
| } | |
| } | |
| $client = new Client(CLIENT_KEY, CLIENT_SECRET); | |
| if (!$client->isTokenValid()) { | |
| $result = $client->login(API_KEY); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment