0

I have a function in controller and I want to call an HTTP request passing url like GET request in AJAX but I couldn't figure how do I use it using php I tried this

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
}
Alen
  • 1,221
  • 5
  • 21
  • 43
  • Possible duplicate of [How to send a GET request from PHP?](http://stackoverflow.com/questions/959063/how-to-send-a-get-request-from-php) – Jirennor Apr 30 '17 at 09:56
  • @ThomasSnijder yess it is possible bro! Thank you :) – Alen Apr 30 '17 at 10:30

1 Answers1

1

you can use Guzzle Package for 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", ...}'
Mortada Jafar
  • 3,529
  • 1
  • 18
  • 33