M
M
milyaevamarishka2015-06-02 13:20:10
PHP
milyaevamarishka, 2015-06-02 13:20:10

How to display api yandex metrics via php?

I want to display attendance data and so on from Yandex metrics in the admin panel, the admin panel is written using php-view-class with smarty. I can't display even outside the admin panel to a clean directory, please tell me an example?
I registered a token, directly, if I insert it into the browser, for example: https://api-metrika.yandex.ru/stat/traffic/summary... it gives data if I enter
https://api-metrika.yandex.ru/stat /traffic/summary...
is empty.
Yandex developer Did not help, and tried many examples.
I really need a class and a php example, preferably working;) if the example immediately on smarty will be generally super.)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GEOgraf, 2015-06-02
@milyaevamarishka

Recently, statistics were also needed, so I wrote a class.
might help...

class yandex{

  const CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  const CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  const COUNTER_ID = '00000000';

  public static function getStat(){
    $token = self::auth();

    if($token){
      return self::getData(
        'https://api-metrika.yandex.ru/stat/traffic/summary.json?id='.self::COUNTER_ID,
        ['Authorization: OAuth '.$token]
      );
    }
  }

  public static function auth(){
    if (!isset($_GET["code"])) {

      //здесь проверим токен в базе, иначе будем отсылать на получение кода

      Header("Location: https://oauth.yandex.ru/authorize?response_type=code&client_id=".self::CLIENT_ID);
      die();
    }

    //делаем запрос для получения токена
    $result = self::postKeys("https://oauth.yandex.ru/token",
      [
        'grant_type'=> 'authorization_code',
        'code'=> $_GET["code"],
        'client_id'=>self::CLIENT_ID,
        'client_secret'=>self::CLIENT_SECRET
      ],
      ['Content-type: application/x-www-form-urlencoded']
    );
    
    if ($result["code"]==200){
      $result["response"]=json_decode($result["response"],true);
      if(isset($result["response"]["access_token"]))
        return $result["response"]["access_token"];
      else
        return false;
    }else{
      return false;
    }

  }

  public static function postKeys($url,$peremen,$headers) {
    $post_arr=array();
    foreach ($peremen as $key=>$value) {
      $post_arr[]=$key."=".$value;
    }
    $data=implode('&',$post_arr);

    $handle=curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    $response=curl_exec($handle);
    $code=curl_getinfo($handle, CURLINFO_HTTP_CODE);
    return ["code"=>$code,"response"=>$response];
  }

  public static function getData($url,$headers) {
    $handle=curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    $response=curl_exec($handle);
    $code=curl_getinfo($handle, CURLINFO_HTTP_CODE);
    return json_decode($response,true);
  }
}
$stat = yandex::getStat();

I
Ilya Korablev, 2015-06-02
@swipeshot

look

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question