- How to Get Current Page URL, Domain, Query String in PHP
- How to Get Current Page URL in PHP
- PHP Program for get Current Page Url
- Program 1 full source Code
- PHP Program to show current page URL
- PHP Program show Current page full URL
- Program 2 full source Code
- PHP Program show Current page full URL
- PHP Program show Current full URL with Query String
- Program 3 full source Code
- PHP Program show Current page full URL with Query String
- Other Important elements of $_SERVER that are very are useful and you must know that:
- Author Admin
- PHP: Using Guzzle to make URL GET and POST Requests
- Installation
- How to use it
- GET Requests
- POST Requests
- Request Options
- Responses
- Exceptions
- Related Posts
How to Get Current Page URL, Domain, Query String in PHP
In this tutorial, We would love to share with you, How to get current page URL in PHP, PHP get current URL path, PHP get full URL with parameters, PHP get a current page, PHP get current URL with query string, PHP get a domain from URL.
How to Get Current Page URL in PHP
Here you will discuss about superglobal variable $_SERVER, this is built-in PHP variable. We will use this variable get the current page URL in PHP. You can get every piece of information about the current URL using the $_SERVER superglobal.
Before we try to fetch the Current Page Full URL lets see how a normal URL structure looks like:
http://www.abc.com/dir1/test.php?glob=hello&name=world
- HTTP: The URL protocol
- www.abc.com – The domain name or the hostname.
- dir1: The directory within the root
- test.php – The actual PHP script
- glob=hello – the first URL parameter (glob) and it’s a value (hello)
- name=world – the second URL parameter (name) and its value (world)
Now we will create a program that is used to get current page url.
PHP Program for get Current Page Url
Program 1 full source Code
PHP Program to show current page URL
PHP Program show Current page full URL
Program 2 full source Code
PHP Program show Current page full URL
PHP Program show Current full URL with Query String
Program 3 full source Code
PHP Program show Current page full URL with Query String
Other Important elements of $_SERVER that are very are useful and you must know that:
- $_SERVER[‘SERVER_ADDR’]: IP address of the server
- $_SERVER[‘REQUEST_METHOD’]: Returns the page access method used i.e. ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’.
- $_SERVER[‘REQUEST_TIME’]: timestamp of the start of the request.
- $_SERVER[‘HTTP_REFERER’]: returns the referrer page uri – used in redirecting to last page after login
- $_SERVER[‘SCRIPT_FILENAME’]: returns the path including the filename, like DIR
- $_SERVER[‘HTTP_COOKIE’]. returns the raw value of the ‘Cookie’ header sent by the user agent.
- $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]): returns the default set language – useful for websites with multilingual content & readers
- $_SERVER[‘HTTP_USER_AGENT’]: returns the kind of device being used to access (desktop/mobile device etc) – suitable for switching interface for different devices.
Author Admin
My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.
PHP: Using Guzzle to make URL GET and POST Requests
Guzzle is a php library to open external urls and retrieve their content. A common application of this library is for accessing the API (Application Program Interface) of other sites. It can also be used to automate the submission of forms, as well as the uploading or downloading of files to/from other sites.
Php already has an extension, the curl extension, that allows to perform the same functionality, however Guzzle provides an object oriented implementation to make url requests and handle their responses in a more efficient way.
Installation
To install Guzzle we can use the composer command:
php composer.phar require guzzlehttp/guzzle:^7.0
Or, if we are adding Guzzle to an existing project using composer, we will add the guzzle package name to our composer.json file:
How to use it
To use Guzzle we would need to first include the vendor/autoload.php script.
Then we will indicate the Guzzle classes that we are going to use, GuzzleHttp\Client and GuzzleHttp\Psr7\Request.
require_once __DIR__ . 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request;
GET Requests
To make a GET request, we start by creating an instance of the Client class.
After that we prepare an instance of the Request class, where we indicate the url we are going to open, and finale we send the request.
send($request); // Read Response $response_body = (string)$response->getBody(); var_dump($response_body);
GET Request with Url Parameters
To send parameters to the url, we have two methods to choose from.
We can put the parameters next to the url. Or we can define them in an array that we pass as an option named query on the send method.
send($request); // Method 2 $request = new Request('GET', 'https://www.example.com'); $response = $client->send($request, [ 'query' => [ 'param1' => 'value1', 'param2' => 'value2' ] ]); // Read Response $response_body = (string)$response->getBody(); var_dump($response_body);
POST Requests
POST Request with Form Fields
To make a POST request that will simulate the submission of a form we will use the form_params option in the send method, and pass the form fields as an associative arrays where the keys are the field names, and the values of the array will be the field values.
send($request, [ 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded', ], 'form_params' => [ 'field1' => 'value1', 'field2' => 'value2' ] ]); // Read Response $response_body = (string)$response->getBody(); var_dump($response_body);
POST Request with Raw Data
We can also send raw data in a POST request. In this case we will pass the data in the body option.
send($request, [ 'body' => 'ABCDEF', ]); // Read Response $response_body = (string)$response->getBody(); var_dump($response_body);
Request Options
When making a request we can specify certain options that will provide additional information or instructions to the request.
Send a Request with Headers
With the headers option we can also send some common headers such as Cache-Control, User-Agent, Accept, and so on.
$response = $client->send($request, [ 'headers' => [ 'Pragma' => 'no-cache', 'Cache-Control' => 'no-cache', 'Upgrade-Insecure-Requests' => '1', 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language' => 'en-US,en;q=0.9' ] ]);
Some other options include the following:
- verify, if true it will check the SSL certificate, else if false it will not check the certificate. This last option is insecure, and could be used, for instance, when testing a local environment with self-signed certificates.
- allow_redirects specify if Guzzle will automatically make a request to the url indicated on the Location header of the response.
- debug, will output information about the connection to the remote url, as well as the response.
$response = $client->send($request, [ 'verify' => false, 'allow_redirects' => false, 'debug' => true, ]);
When you want to send Json data you could use the json option, where you can put the Json data in a associative array.
$response = $client->send($request, [ 'headers' => [ 'Content-Type' => 'application/json' ], 'json' => [ "field1" => "value1", "field2" => "value2", ], ]);
Responses
To retrieve the response of the request you would use the «getBody» method. This method will return a stream, so you may explicitely cast it to a string or just let it be implicitely cast. Alternatively, you could read it as a stream.
$response_body = (string)$response->getBody(); echo $response_body;
You can retrieve the headers of the response with the «getHeader» method. This method returns an array, so you would have to specify the index zero to get the value of the header.
$content_type = $response->getHeader('Content-Type')[0]; echo $content_type;
Get all Response Headers of a specified type
When you have several values for a specific header, you would navigate through the array returned by the «getHeader» method.
$headers = $response->getHeader('Set-Cookie'); foreach ($headers as $header)
To get the full headers of the response use the «getHeaders» method. which return them as an array of arrays.
$all_headers = $response->getHeaders(); foreach ($all_headers as $header_name => $headers) < foreach ($headers as $header) < echo $header_name . ': ' . $header . "\n"; >>
Get the Response’s Status Code
You can also get the Status Code of the Response with the «getStatusCode» method.
$status_code = $response->getStatusCode(); echo $status_code;
Exceptions
Guzzle can also throw its own exceptions, being a couple of useful ones the ConnectException and RequestException.
send($request); > catch (RequestException $e) < echo $e->getMessage() . "\n"; var_dump($e->getRequest()); if ($e->hasResponse()) < var_dump($e->getResponse()); > >