· 5 years ago · Feb 10, 2021, 08:54 AM
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6use GuzzleHttp;
7
8class DictionaryController extends Controller
9{
10 public function index()
11 {
12 $text = $_GET['text'];
13
14 try {
15 if (is_null(config('app.yandex_api_key'))) {
16 echo 'NULL YANDEX API KEY';
17 die();
18 }
19
20 $client = new GuzzleHttp\Client();
21 $res = $client->get('https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=0imfnc8mVLWwsAawjYr4Rx-Af50DDqtlx&lang=en-ru&text=$text', ['headers' => ['X-Yandex-API-Key' => config('app.yandex_api_key')]]);
22 $status = $res->getStatusCode();
23 $dictionary_data = GuzzleHttp\json_decode($res->getBody());
24
25 if ($status != 200) {
26 echo 'ERROR GET DATA';
27 die();
28 }
29
30 return view('dictionary', ['data' => $dictionaryData, 'text' => $text]);
31
32 } catch (\Exception $e) {
33 echo $e;
34 }
35 }
36}
37