Slim框架 - 调用外部API

Gui*_*las 5 api rest curl slim

我是Slim Framework 2的新手,我想对外部API进行HTTP调用.

它只是这样的: GET http://website.com/method

有没有办法使用Slim做到这一点,还是我必须使用curl for PHP?

Law*_*eGS 12

您可以使用Slim Framework构建API.要使用其他API,您可以使用PHP Curl.

例如:

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

?>
Run Code Online (Sandbox Code Playgroud)