hren'

← разместить еще код

Расшаренный код:

<?php

spl_autoload_register(function ($class_name) {
    include 'classes/'.$class_name . '.php';
});
$GoogleSpreadSheets=new GoogleSpreadSheets();
$response = $GoogleSpreadSheets->getSheetData();

$date_start = $GoogleSpreadSheets->getStatisticDate()['start'];
$date_end   = $GoogleSpreadSheets->getStatisticDate()['end'];

$data=[];

$Vk=new Vk();
$Ok=new Ok();
$Facebook=new Facebook();

$Vk->dateFrom=$date_start;
$Vk->dateTo=$date_end;


for ($i=3;$i>0;$i++){

    $range=$i.':'.$i;
    $item_data = $GoogleSpreadSheets->getSpreadSheetFromRange($range);

    if (!empty($item_data)){

        $views = $reach = $likes = $comments = $reposts = $votes = 0;

        switch ($item_data[0][1]){

            case 'ВК':
                $parts = parse_url($item_data[0][4]);
                parse_str($parts['query'], $query);

                $Vk->setPostId(explode("_", str_replace('wall-','',$query['w']))[1]);
                $Vk->setGroupId(explode("_", str_replace('wall-','',$query['w']))[0]);

                $wallStatistic=$Vk->getWallStatistic()['response'][0];

                $reach = $wallStatistic['reach_total'];

                $wallInfo=$Vk->getWallInfo()['response'][0];

                $views = $wallInfo['views']['count'];
                $likes =$wallInfo['likes']['count'];
                $comments =$wallInfo['comments']['count'];
                $reposts =$wallInfo['reposts']['count'];

                if (isset($wallInfo['attachments'])){
                    foreach ($wallInfo['attachments'] as $key_attachments => $val_attachments){
                        if ($val_attachments['type'] !== 'poll')
                            continue;
                        $votes =$wallInfo['attachments'][$key_attachments]['poll']['votes'];
                    }
                }

                $data[$i]=[
                    'date'=>$item_data[0][0],
                    'platform'=>$item_data[0][1],
                    'title'=>$item_data[0][2],
                    'name'=>$item_data[0][3],
                    'href'=>$item_data[0][4],
                    'stats' => [
                        'views' => $views,
                        'reach' => $reach,
                        'likes' => $likes,
                        'comments' => $comments,
                        'reposts' => $reposts,
                        'votes' => $votes,
                    ]
                ];
                $views = $reach = 0;
                break;

            case 'ОК':
                $parts = explode('/',$item_data[0][4]);

                $Ok->setTopicId($parts[5]);

                $ok_statistic=$Ok->getStatTopic();

                $views = '';
                $likes = $ok_statistic['topic']['likes'];
                $reach = $ok_statistic['topic']['reach'];
                $comments = $ok_statistic['topic']['comments'];
                $reposts = $ok_statistic['topic']['reshares'];

                $data[$i]=[
                    'date'=>$item_data[0][0],
                    'platform'=>$item_data[0][1],
                    'title'=>$item_data[0][2],
                    'name'=>$item_data[0][3],
                    'href'=>$item_data[0][4],
                    'stats' => [
                        'views' => $views,
                        'reach' => $reach,
                        'likes' => $likes,
                        'comments' => $comments,
                        'reposts' => $reposts,
                        'votes' => $votes,
                    ]
                ];

                $views = $reach = 0;

                break;

            case 'ФБ':
                break;

            case 'Инстаграм':
                break;

            case 'Дзен':
                break;

            default:
                break;
        }
    }else{

        break;
    }
}
echo '<pre>';
print_r($data);
echo '</pre>';

?>