Good day!
Make an ajax request from js on back. Then you need to send a get request with the third-party api. Can you please tell me how to send request and get response. Watched a bunch of articles, videos and nothing comes out.
1) I Tried to do using Guzzle
use GuzzleHttp\\Client; public function getData() { $client = new Client([ 'headers' => [ 'content-type' => 'application/json', 'Accept' => 'application/json' ], ]); $response = $client->request('GET', ' link ']); $data = $response->getBody(); return response()->json( [ 'response' => [ 'test' => $data ] ], 200); }
2) Tried it this way
public function getData() { $url = " link "; $opts = [ 'http' => [ 'method' => "GET", ] ]; $context = stream_context_create($opts); $data = file_get_contents($url, false, $context); return response()->json( [ 'response' => [ 'test' => $data ] ], 200); }
Checked on 2 API. The first method does not work with either. The second only works with one. Just pasting link in the address bar in the browser get answers from two API. How to send request and receive response.
P. S. working With Laravel for a second week, hence the question. NID help!