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();
CodePudding user response:
I get it, i just need to GET one piece, then Post it , and then get the everything which i posted, but how can i make loop for this? my code now is --> public function fetch() {
$get = Http::get('some api');
$countries = json_decode($get, true);
foreach ($countries as $country)
{
$post = Http::post('some api', [
'country' => $country['name']['en'],
'code' => $country['code'],
])->json();
}
return 'DONE';
}
and i get information for only 1 country, but how to make like 'country' : AL 'code' : BE, and then spawn more of them like, if i have 100 country and codes, to make everything for 100 times and not only for 1?
CodePudding user response:
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;