Created
February 4, 2018 14:36
-
-
Save uptimizt/51f25181091c1f3be4424b2ceb60f0d8 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* AmoCRM API Wrapper | |
*/ | |
class WooAC_API_Wrapper | |
{ | |
protected $curl; | |
protected $url_domain; | |
function __construct() | |
{ | |
$this->url_domain = 'https://' . get_option('wac_subdomain') . '.amocrm.ru'; | |
$login_data = array( | |
'USER_LOGIN' => get_option('wac_login'), | |
'USER_HASH' => get_option('wac_key') | |
); | |
$auth = $this->curlRequest('/private/api/auth.php?type=json', 'POST', $login_data); | |
} | |
function curlRequest($ep, $method = 'GET', $parameters = null, $headers = null, $timeout = 30) | |
{ | |
try { | |
$url = $this->url_domain . $ep; | |
if ($method == 'GET' && is_null($parameters) == false) { | |
$url = add_query_arg($parameters, $url); | |
} | |
// Get curl handler or initiate it | |
if ( ! $this->curl ) { | |
$this->curl = curl_init(); | |
} | |
//Set general arguments | |
curl_setopt($this->curl, CURLOPT_USERAGENT, 'amoCRM-API-client/1.0'); | |
curl_setopt($this->curl, CURLOPT_URL, $url); | |
curl_setopt($this->curl, CURLOPT_FAILONERROR, false); | |
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout); | |
curl_setopt($this->curl, CURLOPT_HEADER, false); | |
curl_setopt($this->curl, CURLOPT_COOKIEFILE, '-'); | |
curl_setopt($this->curl, CURLOPT_COOKIEJAR, '-'); | |
// Reset some arguments, in order to avoid use some from previous request | |
curl_setopt($this->curl, CURLOPT_POST, false); | |
curl_setopt($this->curl, CURLOPT_HTTPHEADER, false); | |
if (is_null($headers) === false) { | |
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); | |
} | |
if ($method == 'POST' && is_null($parameters) === false) { | |
curl_setopt($this->curl, CURLOPT_POST, true); | |
//Encode parameters if them already not encoded in json | |
$parameters = http_build_query( $parameters ); | |
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $parameters); | |
} | |
$response = curl_exec($this->curl); | |
$statusCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); | |
$errno = curl_errno($this->curl); | |
$error = curl_error($this->curl); | |
if ($errno) { | |
throw new Exception($error, $errno); | |
} | |
$result = json_decode($response, true); | |
if ($statusCode >= 400) { | |
throw new Exception($result['message'], $statusCode); | |
} | |
return $result; | |
} catch (Exception $e) { | |
return new WP_Error( 'api_request_error', $e->getMessage() ); | |
} | |
} | |
/** | |
* Do some actions when instance destroyed | |
*/ | |
function __destruct() { | |
//Close curl session | |
curl_close($this->curl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment