retet - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of retet.php

  1. <?php
  2.  
  3. if (!isset($_REQUEST)) {
  4. return;
  5. }
  6.  
  7. //Строка для подтверждения адреса сервера из настроек Callback API
  8. $confirmation_token = '2749a54a';
  9.  
  10. //Ключ доступа сообщества
  11. $token = 'c0223f775444cf3d58a8a1442ec76a9571c8f58e3e24616d9440f73dc43022bbead9b2e576cb41d09c0a1';
  12.  
  13. //Получаем и декодируем уведомление
  14. $data = json_decode(file_get_contents('php://input'));
  15.  
  16. //Проверяем, что находится в поле "type"
  17. switch ($data->type) {
  18. //Если это уведомление для подтверждения адреса...
  19. case 'confirmation':
  20. //...отправляем строку для подтверждения
  21. echo $confirmation_token;
  22. break;
  23.  
  24. //Если это уведомление о новом сообщении...
  25. case 'message_new':
  26. //...получаем id его автора
  27. $user_id = $data->object->user_id;
  28. //затем с помощью users.get получаем данные об авторе
  29. $user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$user_id}&access_token={$token}&v=5.0"));
  30.  
  31. //и извлекаем из ответа его имя
  32. $user_name = $user_info->response[0]->first_name;
  33.  
  34. //С помощью messages.send отправляем ответное сообщение
  35. $request_params = array(
  36. 'message' => "Hello, {$user_name}!",
  37. 'user_id' => $user_id,
  38. 'access_token' => $token,
  39. 'v' => '5.0'
  40. );
  41.  
  42. $get_params = http_build_query($request_params);
  43.  
  44. file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
  45.  
  46. //Возвращаем "ok" серверу Callback API
  47.  
  48. echo('ok');
  49.  
  50. break;
  51.  
  52. }
  53. ?> 
File Description
  • retet
  • PHP Code
  • 08 Aug-2018
  • 1.66 Kb
You can Share it: