1- İlk önce kimlik bilgileri aşağıdaki gibi oluşturacak. link -> https://oauth.yandex.com/client/new
2- Scope -> Yandex.Metrica -> 'Access to statistics and ability to view all counter settings' yukardaki gördüğünüz gibi işaretleyiniz.
3- Doğrulandıktan sonra php ile kodlandığı sayfasına GET ile dönebilecek '' alanındaydı. Ancak birden fazla ekleyemezsiniz.
4- Yandex client için classı yazdım. yandexClient.class.php
class yandexClient { private $client_id = '***********'; private $client_secret = '**********'; private $access_token; function __construct() { } public function createAuthUrl(){ return "https://oauth.yandex.com/authorize?response_type=code&client_id=".$this->client_id; } public function fetchAccessTokenWithAuthCode($code){ $query = array( 'grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $this->client_id, 'client_secret' => $this->client_secret ); $post_fields = http_build_query($query); $rdata = $this->http('https://oauth.yandex.com/token',$post_fields); return json_decode($rdata,true); } function setAccessToken($token){ $_SESSION['access_token'] = $token; $this->access_token = $token; } public function getAccessToken(){ return $this->access_token; } function http($url,$post_fields = null){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if(!empty($post_fields)){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_fields); } $server_output = curl_exec ($ch); curl_close ($ch); return $server_output; } function get($get){ if (!empty($get['startdate']) && !empty($get['enddate'])){ $startDate = $get['startdate']; $endDate = $get['enddate']; } elseif (!empty($get['startdate']) && empty($get['enddate'])){ $startDate = $get['startdate']; $endDate = date('Ymd'); } else { $startDate = date('Ymd',strtotime('-1 months')); $endDate = date('Ymd',strtotime('-1 day')); } //Sayaç id if(!empty($get['id'])) { $id = $get['id']; } else { $id = '37471320'; } if (!empty($get['url'])){ $url = $get['url']; } else { $url = 'stat/traffic/summary'; } $url = "https://api-metrika.yandex.com.tr/$url.json?id=$id&oauth_token=".$this->access_token."&date1=$startDate&date2=$endDate"; return json_decode($this->http($url),true); } function getBrowsers($id){ $startDate = date('Ymd',strtotime('-1 months')); $endDate = date('Ymd'); $url = "https://api-metrika.yandex.com.tr/stat/tech/browsers.json?id=$id&oauth_token=".$this->access_token."&date1=$startDate&date2=$endDate"; return json_decode($this->http($url),true); } function getOperatingSystems($id){ $startDate = date('Ymd',strtotime('-1 months')); $endDate = date('Ymd'); $url = "https://api-metrika.yandex.com.tr/stat/tech/os.json?id=$id&oauth_token=".$this->access_token."&date1=$startDate&date2=$endDate"; return json_decode($this->http($url),true); } function getCountry($id){ $startDate = date('Ymd',strtotime('-1 months')); $endDate = date('Ymd'); $url = "https://api-metrika.yandex.com.tr/stat/geo.json?id=$id&oauth_token=".$this->access_token."&date1=$startDate&date2=$endDate"; return json_decode($this->http($url),true); } }
5- İndex.php
session_start(); require_once 'yandexClient.class.php'; $client = new yandexClient(); if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); $client->setAccessToken($token['access_token']); } if (!empty($_SESSION['access_token'])) { $client->setAccessToken($_SESSION['access_token']); } else { $authUrl = $client->createAuthUrl(); echo '<a href="'.$authUrl.'">Bağlan</a>'; } if ($client->getAccessToken()){ $id = '37471320'; $get = $client->getBrowsers($id); //$get = $client->getOperatingSystems($id); //$get = $client->getCountry($id); //$get = $client->getBrowsers($id); print_r($get);}
Daha bilgi ve tüm fonksiyonlar için;
https://tech.yandex.com.tr/metrika/doc/ref/reference/metrika-api-resources-docpage/
GOSB Teknopark Hi-Tech Bina 3.Kat B3 Gebze - KOCAELİ