$subDomain = 'url';
$link = 'https://' . $subDomain . '.amocrm.ru/oauth2/access_token';
$data = [
'client_id' => 'xxx,
'client_secret' => xxx,
'grant_type' => 'authorization_code',
'code' => 'code',
'redirect_uri' => 'url'
];
$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_USERAGENT,'amoCRM-oAuth-client/1.0');
curl_setopt($curl,CURLOPT_URL, $link);
curl_setopt($curl,CURLOPT_HTTPHEADER,['Content-Type:application/json']);
curl_setopt($curl,CURLOPT_HEADER, false);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, 2);
$out = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = \json_decode($out, true);
$client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'User-Agent' => 'amoCRM-oAuth-client/1.0'
],
]);
$subDomain = 'url';
$link = 'https://' . $subDomain . '.amocrm.ru/oauth2/access_token';
$data = [
'client_id' => 'xxx,
'client_secret' => xxx,
'grant_type' => 'authorization_code',
'code' => 'code',
'redirect_uri' => 'url'
];
try {
$response = $client->request('POST', $link, ['form_params' => $data]);
} catch (GuzzleHttp\Exception\GuzzleException | Exception $e) {
echo $e->getMessage() . \PHP_EOL;
}
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
$user = [
'USER_LOGIN' => $this->getLogin(),
'USER_HASH' => $this->getKey()
];
try {
$response = $this->getClient()->request('Post', '/private/api/auth.php?type=json', [
'headers' => [
'User-Agent' => 'amoCRM-API-client/1.0',
'Content-Type' => 'application/json'
],
RequestOptions::JSON => $user
]);
} catch (GuzzleException | Exception $e) {
echo $e->getMessage() . \PHP_EOL;
exit;
}
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Exception;
$client = new Client();
$subdomain = 'subdomain'; //Поддомен нужного аккаунта
$link = 'https://' . $subdomain . '.amocrm.ru/oauth2/access_token'; //Формируем URL для запроса
/** Соберем данные для запроса */
$data = [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'grant_type' => 'authorization_code',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
];
try {
$response = $client->request('POST', $link, [
'headers' => [
'Content-Type' => 'application/json',
'User-Agent' => 'amoCRM-oAuth-client/1.0'
],
RequestOptions::JSON => $data
]);
$resp = json_decode($response->getBody()->getContents()); # возвращается у нас stdClass, и обращение к элементам идет $resp->token_type, $resp->expires_in, $resp->access_token, $resp->refresh_token
print_r($resp->token_type);
print_r($resp->expires_in);
print_r($resp->access_token);
print_r($resp->refresh_token);
} catch (GuzzleException | Exception $e) {
print_r($e->getMessage());
}