I'm completely new to Slim Framework 2 and I would like to make an HTTP call to an external API.
It would simply something like:
GET http://website.com/method
Is there a way to do this using Slim or do I have to use curl for PHP?
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
Installing Slim FrameworkAdd the Slim Framework dependency to composer. json (in my case it creates the file for me as I don't already have one, it's safe to run this if you do already have a composer. json file) Run composer install so that those dependencies are actually available to use in your application.
You can build an API using Slim Framework. To consume other 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);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With