Created
February 18, 2015 11:48
-
-
Save DmitryDmitrienko/e202ce9de5c3d232fef1 to your computer and use it in GitHub Desktop.
Bitrix24 API
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
/* КОНФИГИ ДЛЯ ПРИЛОЖЕНИЯ */ | |
GLOBAL $CONGIG_BX24_INTERVOLGA; | |
$CONGIG_BX24_INTERVOLGA = array( | |
'client_id' => 'bc5f97fab2d24d39724faef110c7b10a', | |
'secret' => '32aa9879cf1a4e2b6d6c7db5c2a65f26', | |
'domain' => 'example.bitrix24.ru', | |
'redirect_uri' => 'http://127.0.0.1/company/index.php/', | |
'scope' => array('user', 'task') | |
); | |
/// | |
// Класс наследник от API https://github.com/mesilov/bitrix24-php-sdk | |
class IntervolgaApiBitrix24 extends \Bitrix24\Bitrix24 { | |
// конфигурация приложения | |
private $conf = array(); | |
function __construct(array $conf = array()){ | |
parent::__construct(); | |
$this->conf = $conf; | |
// получаем токены из свойств пользователя и устанавливаем их значение в память текущего объекта | |
$this->getTokenFromUser(); | |
// устанавливаем в свойства текущего объекта конфигурационные данные | |
$this->setApplicationScope($this->conf['scope']); | |
$this->setApplicationId($this->conf['client_id']); | |
$this->setApplicationSecret($this->conf['secret']); | |
$this->setRedirectUri($conf['redirect_uri']); | |
$this->setDomain($this->conf['domain']); | |
} | |
/** | |
* Изменить конфигурацию приложения | |
*/ | |
function setIntervolgaConf(array $conf = array()){ | |
$this->conf = $conf; | |
} | |
/** | |
* Сгенерировать код доступа к API | |
*/ | |
function generateToken($code){ | |
// склеим url для получения токена | |
$url_token = "https://".$this->conf['domain'].'/oauth/token/?client_id='. | |
$this->conf['client_id']."&grant_type=authorization_code&client_secret=". | |
$this->conf['secret']."&redirect_uri=".urldecode($this->conf['redirect_uri'])."&code=".$code."&scope=". | |
implode(",", $this->conf['scope']); | |
$res = $this->executeRequest($url_token); | |
$this->setApplicationScope($this->conf['scope']); | |
$this->setApplicationId($this->conf['client_id']); | |
$this->setApplicationSecret($this->conf['secret']); | |
$this->setDomain($this->conf['domain']); | |
$this->setTokens($res); | |
} | |
/** | |
* Перезаписать код доступа к API | |
*/ | |
function refreshToken(){ | |
$this->getNewAccessToken(); | |
} | |
/** | |
* Установить новые токены в дополнительные поля свойств пользователя | |
* Установить новые токены в свойства объекта | |
*/ | |
private function setTokens(array $arrData = array()){ | |
global $USER; | |
$user = new CUser; | |
$user->Update($USER->GetID(), array( | |
"UF_BITRIX24_TOKEN" => $arrData['access_token'], | |
"UF_REFRESH_TOKEN" => $arrData['refresh_token'] | |
)); | |
$this->setMemberId($arrData['member_id']); | |
$this->setAccessToken($arrData['access_token']); | |
$this->setRefreshToken($arrData['refresh_token']); | |
} | |
/** | |
* Установить в текущий объект токены из дополнительный свойств пользователя | |
*/ | |
private function getTokenFromUser(){ | |
global $USER; | |
$rsUser = CUser::GetList( | |
($by="ID"), | |
($order="desc"), | |
array("ID"=>$USER->GetID()), | |
array("SELECT"=>array("UF_BITRIX24_TOKEN", "UF_REFRESH_TOKEN")) | |
); | |
$res = $rsUser->Fetch(); | |
if (!is_null($res['UF_BITRIX24_TOKEN'])) | |
$this->setAccessToken($res['UF_BITRIX24_TOKEN']); | |
if(!is_null($res['UF_REFRESH_TOKEN'])) | |
$this->setRefreshToken($res['UF_REFRESH_TOKEN']); | |
} | |
}; | |
// Пример работы | |
$code_for_token = ''; // токен | |
$in = NULL; | |
// если пользователь прошел редирект на страницу с кодом для получения токена | |
if(isset($_GET['code']) == true) { | |
$code_for_token = $_GET['code']; | |
$in = new IntervolgaApiBitrix24($CONGIG_BX24_INTERVOLGA); | |
$in->generateToken($code_for_token); | |
} | |
else{ | |
// | |
} | |
$in = new IntervolgaApiBitrix24($CONGIG_BX24_INTERVOLGA); | |
if(!$in->isAccessTokenExpire()){ | |
$userB24 = new \Bitrix24\User\User($in); | |
$users_arr = $userB24->get(); | |
} | |
else{ | |
$result = $in->getNewAccessToken(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment