- Send GET request with PHP.
- Using file_get_contents.
- Send a GET request with cURL.
- PHP GET/POST request
- HTTP
- HTTP GET
- HTTP POST
- PHP $_GET and $_POST
- PHP GET request
- PHP POST request
- PHP send GET request with Symfony HttpClient
- PHP send POST request with Symfony HttpClient
- PHP GET request in Symfony
- PHP POST request in Symfony
- PHP GET request in Slim
- PHP POST request in Slim
- PHP GET request in Laravel
- PHP POST request in Laravel
- Author
Send GET request with PHP.
This is a beginner’s tutorial on how to send a simple HTTP GET request with PHP. To send a GET request via PHP, there are two different methods that we can use.
Using file_get_contents.
The first method involves using the function file_get_contents.
This easy-to-use function has been present since PHP version 4.3.0 and its purpose is to “read an entire file into a string.” In this case, the file in question is a URL that we will be accessing via GET.
Take a look at the following sample code.
//The URL that we want to GET. $url = 'http://google.com'; //Use file_get_contents to GET the URL in question. $contents = file_get_contents($url); //If $contents is not a boolean FALSE value. if($contents !== false)< //Print out the contents. echo $contents; >
When supplied with a URL, file_get_contents will retrieve the contents of the URL using a HTTP GET request.
If you need to attach a query string / GET parameters, then you can simply add them onto the end of the URL like so.
//The URL with parameters / query string. $url = 'http://google.com?id=1&name=wayne'; //Once again, we use file_get_contents to GET the URL in question. $contents = file_get_contents($url); //If $contents is not a boolean FALSE value. if($contents !== false)< //Print out the contents. echo $contents; >
In the code above, we set the query string parameters id and name.
Unfortunately, the drawback of using file_get_contents is that you can’t retrieve HTTP response codes and error handling is extremely basic.
Send a GET request with cURL.
To retrieve HTTP response codes and other header information, you can use the cURL functions. By default, cURL uses GET requests.
//Initialize cURL. $ch = curl_init(); //Set the URL that you want to GET by using the CURLOPT_URL option. curl_setopt($ch, CURLOPT_URL, 'http://google.com'); //Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Set CURLOPT_FOLLOWLOCATION to true to follow redirects. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Execute the request. $data = curl_exec($ch); //Close the cURL handle. curl_close($ch); //Print the data out onto the page. echo $data;
- Initialized cURL using the curl_init function.
- We set the URL that we wanted to GET using the CURLOPT_URL parameter.
- We set the CURLOPT_RETURNTRANSFER option to true so that the contents of the URL isn’t automatically printed out onto the page (instead, it is returned as a string by the curl_exec function).
- After that, we set the CURLOPT_FOLLOWLOCATION option to true because we want to follow any redirects that the site tells us to follow. If you want to ignore header redirects, then simply remove the line in question or set the option to false.
- We executed the GET request using curl_exec.
- We closed our cURL handle.
- Finally, we printed the contents of the URL out onto the page.
cURL is the best option to use if you want more control over your HTTP requests. Not only does it have better error handling, it also allows you to use cookies, set referrer information and set custom user agents, etc.
PHP GET/POST request
PHP GET/POST request tutorial shows how to generate and process GET and POST requests in PHP. We use plain PHP and Symfony, Slim, and Laravel frameworks.
$ php -v php -v PHP 8.1.2 (cli) (built: Aug 8 2022 07:28:23) (NTS) .
HTTP
The is an application protocol for distributed, collaborative, hypermedia information systems. HTTP protocol is the foundation of data communication for the World Wide Web.
HTTP GET
The HTTP GET method requests a representation of the specified resource.
- should only be used to request a resource
- parameters are displayed in the URL
- can be cached
- remain in the browser history
- can be bookmarked
- should never be used when dealing with sensitive data
- have length limits
HTTP POST
The HTTP POST method sends data to the server. It is often used when uploading a file or when submitting a completed web form.
- should be used to create a resource
- parameters are not displayed in the URL
- are never cached
- do not remain in the browser history
- cannot be bookmarked
- can be used when dealing with sensitive data
- have no length limits
PHP $_GET and $_POST
PHP provides the $_GET and $_POST superglobals. The $_GET is an associative array of variables passed to the current script via the URL parameters (query string). The $_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
PHP GET request
In the following example, we generate a GET request with curl tool and process the request in plain PHP.
$message = $_GET['message']; if ($message == null) < $message = 'hello there'; >echo "$name says: $message";
The example retrieves the name and message parameters from the $_GET variable.
$ php -S localhost:8000 get_req.php
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there
We send two GET requests with curl.
PHP POST request
In the following example, we generate a POST request with curl tool and process the request in plain PHP.
$message = $_POST['message']; if ($message == null) < $message = 'hello there'; >echo "$name says: $message";
The example retrieves the name and message parameters from the $_POST variable.
$ php -S localhost:8000 post_req.php
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau
We send a POST request with curl.
PHP send GET request with Symfony HttpClient
Symfony provides the HttpClient component which enables us to create HTTP requests in PHP.
$ composer req symfony/http-client
We install the symfony/http-client component.
request('GET', 'http://localhost:8000', [ 'query' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n";
The example sends a GET request with two query parameters to localhost:8000/get_request.php .
$ php -S localhost:8000 get_req.php
$ php send_get_req.php Lucia says: Cau
We run the send_get_req.php script.
PHP send POST request with Symfony HttpClient
In the following example, we send a POST request with Symfony HttpClient.
request('POST', 'http://localhost:8000', [ 'body' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n";
The example sends a POST request with two parameters to localhost:8000/post_req.php .
$ php -S localhost:8000 post_req.php
$ php send_post_req.php Lucia says: Cau
We run the send_post_req.php script.
PHP GET request in Symfony
In the following example, we process a GET request in a Symfony application.
$ symfony new symreq $ cd symreq
A new application is created.
$ composer req annot $ composer req maker --dev
We install the annot and maker components.
$ php bin/console make:controller HomeController
We create a new controller.
) */ public function index(Request $request): Response < $name = $request->query->get('name', 'guest'); $message = $request->query->get('message', 'hello there'); $output = "$name says: $message"; return new Response($output, Response::HTTP_OK, ['content-type' => 'text/plain']); > >
Inside the HomeController’s index method, we get the query parameters and create a response.
$name = $request->query->get('name', 'guest');
The GET parameter is retrieved with $request->query->get . The second parameter of the method is a default value which is used when no value was retrieved.
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau
We generate a GET request with curl.
PHP POST request in Symfony
In the following example, we process a POST request in a Symfony application.
) */ public function index(Request $request): Response < $name = $request->request->get('name', 'guest'); $message = $request->request->get('message', 'hello there'); $output = "$name says: $message"; return new Response($output, Response::HTTP_OK, ['content-type' => 'text/plain']); > >
We change the controller to process the POST request.
$name = $request->request->get('name', 'guest');
The POST parameter is retrieved with $request->request->get . The second parameter of the method is a default value which is used when no value was retrieved.
$ curl -d "name=Lucia" localhost:8000 Lucia says: hello there
We generate a POST request with curl.
PHP GET request in Slim
In the following example, we are going to process a GET request in the Slim framework.
$ composer req slim/slim $ composer req slim/psr7 $ composer req slim/http
We install slim/slim , slim/psr7 , and slim/http packages.
get('/', function (Request $request, Response $response): Response < $name = $request->getQueryParam('name', 'guest'); $message = $request->getQueryParam('message', 'hello there'); $output = "$name says $message"; $response->getBody()->write($output); return $response; >); $app->run();
We get the parameters and return a response in Slim.
$name = $request->getQueryParam('name', 'guest');
The query parameter is retrieved with getQueryParam ; the second parameter is the default value.
$response->getBody()->write($output);
We write the output to the response body with write .
$ php -S localhost:8000 -t public
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau
We generate a GET request with curl.
PHP POST request in Slim
In the following example, we are going to process a POST request in the Slim framework.
post('/', function (Request $request, Response $response): Response < $data = $request->getParsedBody(); $name = $data['name']; $message = $data['message']; if ($name == null) < $name = 'guest'; >if ($message == null) < $message = 'hello there'; >$output = "$name says: $message"; $response->getBody()->write($output); return $response; >); $app->run();
We get the POST parameters and return a response in Slim.
$data = $request->getParsedBody();
The POST parameters are retrieved with getParsedBody .
$ php -S localhost:8000 -t public
$ curl -d "name=Lucia" localhost:8000 Lucia says: hello there
We generate a POST request with curl.
PHP GET request in Laravel
In the following example, we process a GET request in Laravel.
$ laravel new larareq $ cd larareq
We create a new Laravel application.
query('name', 'guest'); $message = $request->query('message', 'hello there'); $output = "$name says $message"; return $output; >);
We get the GET parameters and create a response.
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says Cau
We send a GET request with curl.
PHP POST request in Laravel
In the following example, we send a POST request from an HTML form.
We have a POST form in a Blade template. Laravel requires CSRF protection for POST requests. We enable CSRF protection with @csrf .
); Route::post('/process_form', function (Request $request) < $request->validate([ 'name' => 'required|min:2', 'message' => 'required|min:3' ]); $name = $request->input('name'); $message = $request->input('message'); $output = "$name says: $message"; return $output; >);
We validate and retrieve the POST parameters and send them in the response. This example should be tested in a browser.
In this article we have worked with GET and POST requests in plain PHP, Symfony, Slim, and Laravel.
Author
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.