Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim framework - Call external API

Tags:

rest

curl

api

slim

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?

like image 521
Guilhem Soulas Avatar asked Apr 11 '13 12:04

Guilhem Soulas


People also ask

Is Slim a framework?

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

How do I run a slim framework?

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.


1 Answers

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);

?>
like image 168
LawrenceGS Avatar answered Sep 25 '22 14:09

LawrenceGS