Advertisement
Carsak

Dadata

Mar 24th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. class Dadata
  2. {
  3.     public function suggest($type, $fields)
  4.     {
  5.         $result = false;
  6.         if ($ch = curl_init("http://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/$type"))
  7.         {
  8.             curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  9.             curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  10.                 'Content-Type: application/json',
  11.                 'Accept: application/json',
  12.                 'Authorization: Token ertert'
  13.             ));
  14.             curl_setopt($ch, CURLOPT_POST, 1);
  15.             // json_encode
  16.             curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  17.             $result = curl_exec($ch);
  18.             $result = json_decode($result);
  19.             curl_close($ch);
  20.         }
  21.  
  22.         return $result;
  23.     }
  24.  
  25.     public function getFirstElement($type, $fields)
  26.     {
  27.         $result = $this->suggest($type, $fields);
  28.  
  29.         return $result[0];
  30.     }
  31. }
  32.  
  33. // Как сделать так?
  34. $dadata = new Dadata();
  35. $dadata->suggest($type, $fields)->first(); // Должен возвратить первый элемент массива. Как getFirstElement выше
  36. $dadata->suggest($type, $fields)->all(); // Должен возвратить весь результат, как suggest выше
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement