Create a gist now

Instantly share code, notes, and snippets.

@nalgeon /README.md
Last active Oct 11, 2017

What would you like to do?
Пример работы с подсказками DaData на PHP

Подсказки на PHP

Для большинства PHP-приложений достаточно использовать jQuery-плагин. Он проще в подключении и красиво выглядит. Используйте прямую работу с API на PHP только в том случае, если вы твёрдо уверены, что jQuery-плагин не подходит.

<?php
require('SuggestClient.php');
use Dadata\SuggestClient as SuggestClient;
$token = 'ВАШ API-ключ';
$dadata = new SuggestClient($token);
$query = "7719402047";
$resp = $dadata->suggest("party", $query);
print "Query: " . $query . "\n";
print "Suggestions: \n";
foreach ($resp->suggestions as $suggestion) {
print $suggestion->unrestricted_value . "\n";
}
?>
<?php
namespace Dadata;
class SuggestClient {
private $url,
$token;
public function __construct($token, $url = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/') {
$this->token = $token;
$this->url = $url;
}
public function suggest($resource, $data) {
$options = array(
'http' => array(
'method' => 'POST',
'header' => array(
'Content-type: application/json',
'Authorization: Token ' . $this->token,
),
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($this->url . $resource, false, $context);
return $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment