Hello - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
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.
Result of php executing
Full code of Hello.php
- <?php
- if (!empty($_POST['text']) && is_string($_POST['text'])) {
- header('content-type: application/json'); // говорим что контент JSON
- do {
- $ch = curl_init('http://ahunter.ru/site/suggest/address?addresslim=3;output=json|pretty;query=' . urlencode($_POST['text']));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, 2); // 2 секунды на запрос
- $response = curl_exec($ch);
- if (!is_string($response)) {
- $msg = 'Неизвестная ошибка';
- if (curl_errno($ch) !== 0) {
- $msg = curl_error($ch);
- }
- echo json_decode(['status' => false, 'error' => 'Ошибка обращения к API AHunter: ' . $msg]);
- break;
- }
- curl_close($ch);
- try {
- $data = json_decode($response, true);
- if (!isset($data['suggestions'])) {
- throw new \RuntimeException('в JSON отсутствует массив suggestions');
- }
- } catch (\JsonException|\RuntimeException $e) {
- $msg = $e instanceof \JsonException
- ? 'не удалось распарсить JSON'
- : $e->getMessage();
- echo json_decode(['status' => false, 'error' => 'Ошибка обращения к API AHunter: ' . $msg]);
- break;
- }
- echo json_decode(['status' => true, 'data' => $data['suggestions']]);
- } while (false);
- exit;
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Подсказки</title>
- </head>
- <style>
- input {
- padding: 10px;
- }
- .city1 {
- display: none;
- margin: 5px;
- padding: 10px;
- width: 465px;
- }
- .city2 {
- display: none;
- margin: 5px;
- padding: 10px;
- width: 465px;
- }
- .city3 {
- display: none;
- margin: 5px;
- padding: 10px;
- width: 465px;
- }
- .city1:hover {
- cursor: pointer;
- background-color: #d9d9d9;
- }
- .city2:hover {
- cursor: pointer;
- background-color: #d9d9d9;
- }
- .city3:hover {
- cursor: pointer;
- background-color: #d9d9d9;
- }
- .button {
- text-decoration: none;
- padding: 10px;
- border: 1px solid black;
- color: #000;
- transition: 0.7s;
- }
- .button:hover {
- background-color: #d2d2d2;
- transition: 0.7s;
- }
- </style>
- <body>
- <form method="post">
- <input size="70" id="input" value="" type="text" placeholder="Введите город:" />
- <a href="#" class="button">Очистить</a>
- </form>
- <div class="city1"></div>
- <div class="city2"></div>
- <div class="city3"></div>
- <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
- <script>
- window.addEventListener('DOMContentLoaded', function() {
- var input = document.querySelector('input#input');
- input.addEventListener('input', function(e) {
- // Избавляемся от пустых запросов к API
- if (e.target.value === '' || e.target.value === null) {
- return;
- }
- $.ajax({
- type: 'POST',
- url: '',
- dataType: 'JSON', // говорим JQUERY что ждём JSON
- data: {
- text: e.target.value
- },
- success: function(r) {
- if (r.status) {
- var suggestions = r.data['suggestions'];
- console.log(suggestions);
- } else {
- alert(r.error);
- }
- }
- });
- });
- });
- </script>
- </body>
- </html>