Skip to content

Instantly share code, notes, and snippets.

@nalgeon nalgeon/
Last active Dec 3, 2019

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

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

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

use Dadata\SuggestClient as SuggestClient;
$token = 'ВАШ API-ключ';
$dadata = new SuggestClient($token);
$query = "7719402047";
$data = array(
'query' => $query
$resp = $dadata->suggest("party", $data);
print "Query: " . $query . "\n";
print "Suggestions: \n";
foreach ($resp->suggestions as $suggestion) {
print $suggestion->unrestricted_value . "\n";
namespace Dadata;
class SuggestClient {
private $url,
public function __construct($token, $url = '') {
$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 json_decode($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.