Hello - 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 Hello.php

  1. <?php
  2.  
  3. if (!empty($_POST['text']) && is_string($_POST['text'])) {
  4.     header('content-type: application/json'); // говорим что контент JSON
  5.     
  6.     do {
  7.         $ch = curl_init('http://ahunter.ru/site/suggest/address?addresslim=3;output=json|pretty;query=' . urlencode($_POST['text']));
  8.     
  9.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  10.         curl_setopt($ch, CURLOPT_URL, $url);
  11.         curl_setopt($ch, CURLOPT_TIMEOUT, 2); // 2 секунды на запрос
  12.     
  13.         $response = curl_exec($ch);
  14.         
  15.         if (!is_string($response)) {
  16.             $msg = 'Неизвестная ошибка';
  17.             
  18.             if (curl_errno($ch) !== 0) {
  19.                 $msg = curl_error($ch);
  20.             }
  21.             
  22.             echo json_decode(['status' => false, 'error' => 'Ошибка обращения к API AHunter: ' . $msg]);
  23.             break;
  24.         }
  25.     
  26.         curl_close($ch);
  27.     
  28.         try {
  29.             $data = json_decode($response, true);
  30.             
  31.             if (!isset($data['suggestions'])) {
  32.                 throw new \RuntimeException('в JSON отсутствует массив suggestions');
  33.             }
  34.         } catch (\JsonException|\RuntimeException $e) {
  35.             $msg = $e instanceof \JsonException
  36.                 ? 'не удалось распарсить JSON'
  37.                 : $e->getMessage();
  38.             
  39.             echo json_decode(['status' => false, 'error' => 'Ошибка обращения к API AHunter: ' . $msg]);
  40.             break;
  41.         }
  42.         
  43.         echo json_decode(['status' => true, 'data' => $data['suggestions']]);
  44.     } while (false);
  45.     
  46.     exit;
  47. }
  48.  
  49. ?>
  50.  
  51. <!DOCTYPE html>
  52. <html lang="en">
  53.  
  54. <head>
  55.     <meta charset="UTF-8" />
  56.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  57.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  58.     <title>Подсказки</title>
  59. </head>
  60. <style>
  61.     input {
  62.         padding: 10px;
  63.     }
  64.  
  65.     .city1 {
  66.         display: none;
  67.         margin: 5px;
  68.         padding: 10px;
  69.         width: 465px;
  70.     }
  71.  
  72.     .city2 {
  73.         display: none;
  74.         margin: 5px;
  75.         padding: 10px;
  76.         width: 465px;
  77.     }
  78.  
  79.     .city3 {
  80.         display: none;
  81.         margin: 5px;
  82.         padding: 10px;
  83.         width: 465px;
  84.     }
  85.  
  86.     .city1:hover {
  87.         cursor: pointer;
  88.         background-color: #d9d9d9;
  89.     }
  90.  
  91.     .city2:hover {
  92.         cursor: pointer;
  93.         background-color: #d9d9d9;
  94.     }
  95.  
  96.     .city3:hover {
  97.         cursor: pointer;
  98.         background-color: #d9d9d9;
  99.     }
  100.  
  101.     .button {
  102.         text-decoration: none;
  103.         padding: 10px;
  104.         border: 1px solid black;
  105.         color: #000;
  106.         transition: 0.7s;
  107.     }
  108.  
  109.     .button:hover {
  110.         background-color: #d2d2d2;
  111.         transition: 0.7s;
  112.     }
  113. </style>
  114.  
  115. <body>
  116.     <form method="post">
  117.         <input size="70" id="input" value="" type="text" placeholder="Введите город:" />
  118.         <a href="#" class="button">Очистить</a>
  119.     </form>
  120.     <div class="city1"></div>
  121.     <div class="city2"></div>
  122.     <div class="city3"></div>
  123.     <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
  124.     <script>
  125.         window.addEventListener('DOMContentLoaded', function() {
  126.             var input = document.querySelector('input#input');
  127.             input.addEventListener('input', function(e) {
  128.                 // Избавляемся от пустых запросов к API
  129.                 if (e.target.value === '' || e.target.value === null) {
  130.                     return;
  131.                 }
  132.                 $.ajax({
  133.                     type: 'POST',
  134.                     url: '',
  135.                     dataType: 'JSON', // говорим JQUERY что ждём JSON
  136.                     data: {
  137.                         text: e.target.value
  138.                     },
  139.                     success: function(r) {
  140.                         if (r.status) {
  141.                             var suggestions = r.data['suggestions'];
  142.                             console.log(suggestions);
  143.                         } else {
  144.                             alert(r.error);
  145.                         }
  146.                     }
  147.                 });
  148.             });
  149.         });
  150.     </script>
  151. </body>
  152.  
  153. </html>
  154.  
File Description
  • Hello
  • PHP Code
  • 09 Aug-2022
  • 4.25 Kb
You can Share it: