Fine structure - calling an external API

I am completely new to Slim Framework 2 and I would like to make an HTTP call to an external API.

It's just something like: GET http://website.com/method

Is there a way to do this with Slim, or do I need to use curl for PHP?

+4
source share
2 answers

You can create an API using the Slim Framework. To use a different API, you can use PHP Curl.

So for example:

 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://website.com/method"); curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result // Fetch and return content, save it. $raw_data = curl_exec($ch); curl_close($ch); // If the API is JSON, use json_decode. $data = json_decode($raw_data); var_dump($data); ?> 
+9
source
 <?php try { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://website.com/method"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TCP_KEEPALIVE, 1); curl_setopt($ch, CURLOPT_TCP_KEEPIDLE, 2); $data = curl_exec($ch); if(curl_errno($ch)){ throw new Exception(curl_error($ch)); } curl_close($ch); $data = json_decode($data); var_dump($data); } catch(Exception $e) { // do something on exception } ?> 
0
source

All Articles