При разработке очередного лендинга встала задача — вывести на сайт отзывы об услуге клиента с его официальной группы ВКонтакте.
Такой номер можно сделать благодаря VK.API https://api.vk.com/method/board.getCommentsКод скрипта ниже (прошу не обращать внимание на способ кеширования, мне нужно было это сделать быстро):
<? function vk_query($url, $params) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_IPRESOLVE, CURLOPT_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); $result = curl_exec($ch); curl_close($ch); return $result; } $cash_time = file_get_contents('cash_time.txt'); if (time() > $cash_time){ $result = json_decode(vk_query('https://api.vk.com/method/board.getComments', array( 'group_id' => '79273040', 'topic_id' => '30800886', 'count' => '15', 'extended' => '1', 'sort' => 'desc' )), true); $update = false; $profil_arr = array(); foreach ($result['response']['profiles'] as $profil){ $profil_arr[$profil['uid']] = $profil; $update = true; } $html = ' <div align="center"> <div class="d_table vk_comment" style="margin-top: 50px; width:600px;">'; foreach ($result['response']['comments'] as $comment){ if (!empty($comment['from_id'])){ $profil = $profil_arr[$comment['from_id']]; $text = $comment['text']; preg_match('@\[(.*)\:(.*)\|(.*)\]@', $text, $m); if (!empty($m[0])){ $uid = $m[1]; $name = $m[3]; $r = '<a class="vk_fio" href="https://vk.com/'.$uid.'" target="_blank">'.$name.'</a>'; } $text = preg_replace('@\[.*\]@', $r, $text); $html .= ' <div class="d_tr"> <div class="d_td vk_img"><img src="'.$profil['photo'].'" width="50" height="50" border="0"></div> <div class="d_td">'; if ($comment['from_id'] != 101){ $html .= '<div class="vk_fio"><a class="vk_fio" href="https://vk.com/id'.$comment['from_id'].'" target="_blank">'.$profil['first_name'].' '.$profil['last_name'].'</a></div>'; } else { $html .= '<div class="vk_fio">'.$profil['first_name'].' '.$profil['last_name'].'</div>'; } $html .= '<div class="vk_text">'.$text.'</div> <div class="vk_data">'.date('d.m.Y H:i', $comment['date']).'</div> </div> </div>'; } } $html .= ' </div> </div>'; if ($update == true){ $cash_live = time() + 60*60; file_put_contents('cash_time.txt', $cash_live); file_put_contents('cash_vk.html', $html); echo $html; } else { $cash_html = file_get_contents('cash_vk.html'); echo $cash_html; } } else { $cash_html = file_get_contents('cash_vk.html'); echo $cash_html; } ?>
Facebook Comments
Классно получилось! А как можно еще добавить поле для ввода нового комментария на сайте в группу? В API нашел функцию board.createComment, но как воспользоваться понятия не имею