2

i have made a get request to the route called /fetch, where i am getting some information from the api and automatically save it to database, also i have another piece of api, which needed to POST this piece of information and also store it to database with first piece of code, so, how can i do this?

i created another rouute called Route::post and then in the controller make post function also and bring this code below, when i Die and Dump this i get NULL, but when i die and dump $post without ->json() or decode or something, i get some codes, but i cannot find some info from api in it (?) how can i do this? and also do i need another route for this? or can i use previuos route which i used for get request ?

$post = Http::post('some api')->json();

1 Answer 1

3

You can try the following code

$uri = "https://example.com/api/your-api-url-here";
$params['headers'] = [
     'Content-type' => 'application/json'
];
$params['body'] = [
     'username' => 'helloWorld'
];
$client = new Client();
$response = $client->request('POST', $uri, $params);
$data = json_decode($response->getBody(), true);
dd($data);

And don't forget to use Guzzle/Http in your project, you simply can install guzzle http and use it in your controller like

use GuzzleHttp\Client;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but it didnt work, i used this ---> $url = 'api'; $client = new Client(); $response = $client->request('POST', $url, [ 'form_params' => [ 'code' => $country['code'], ], ]); $data = json_decode($response->getBody(), true); dd($data); but it still doesnt work, it send only for one country, but need it to loop and send like 100 times for each country i have
Result is for only 1 country code, but i need to see result for ALL country codes i have

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.