I'm building an app that interacts with an API. I use cURL to get the json data. I'm using Slim and will use cURL inside the routes, to get the requested data of course. But I'm unsure how to go about this. Should I open and close the cURL object inside each route?
I'm trying to create as few routes as possible. So I'm doing checks inside the route to see what it is supposed to do, that way I can use the same route for multiple things inside of creating many routes that do nearly the same thing. But I wont be able to do that for all of them. So this is what I have at the moment.
$app->get("/{region}/{action}/{id}", function($request, $response, $args) use($app) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "url");
$result = curl_exec($curl);
curl_close($curl);
print_r(json_decode($result));
});
$app->get("/eu/status/{id}", function($request, $response, $args) use($app) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "url");
$result = curl_exec($curl);
curl_close($curl);
print_r(json_decode($result));
});
But it doesn't look right to create a new curl object every time. Any ideas on how I should structure it instead?