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
}
2
  • Possible duplicate of How to send a GET request from PHP? Commented Apr 30, 2017 at 9:56
  • @ThomasSnijder yess it is possible bro! Thank you :) Commented Apr 30, 2017 at 10:30

1 Answer 1

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", ...}'
Sign up to request clarification or add additional context in comments.

1 Comment

Do I have to like require anything? like composer require gizzle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.