我在控制器中有一个函数,我想在AJAX中调用一个像GET请求一样传递url的HTTP请求,但是我无法想象如何使用php我尝试使用它
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
}
答案 0 :(得分:1)
您可以将Guzzle Package用于Laravel,Guzzle PHP
$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", ...}'