Php 如何在控制器函数中调用API
我在controller中有一个函数,我想调用一个HTTP请求传递url,比如AJAX中的GET请求,但我不知道如何使用php使用它。我尝试过这个Php 如何在控制器函数中调用API,php,laravel,get,Php,Laravel,Get,我在controller中有一个函数,我想调用一个HTTP请求传递url,比如AJAX中的GET请求,但我不知道如何使用php使用它。我尝试过这个 public function sendAPIRequest() { $url = "https://myapi.com?apikey=".$api."&message=".$message."&receiver=".$receiver; //This is what I tried file_get_cont
public function sendAPIRequest()
{
$url = "https://myapi.com?apikey=".$api."&message=".$message."&receiver=".$receiver;
//This is what I tried
file_get_contents($url);
//But it didn't work
}
你可以用在拉威尔身上
可能是@ThomasSnijder yess的复制品可能是兄弟!谢谢:)我需要什么吗?喜欢作曲的人需要小发明
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', "https://myapi.com?apikey=".$api."&message=".$message."&receiver=".$receiver);
echo $res->getStatusCode();
// 200
echo $res->getHeaderLine('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// '{"id": 1420053, "name": "guzzle", ...}'