Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data from the URL in PHP?

Tags:

php

say my URL is:

someplace.com/products.php?page=12

How can I then get the information regarding page being equal to 12?

like image 815
jmasterx Avatar asked Aug 01 '11 17:08

jmasterx


People also ask

How get data from API URL in PHP?

Code to read API data using PHP file_get_contents() function PHP inbuilt file_get_contents() function is used to read a file into a string. This can read any file or API data using URLs and store data in a variable. This function is the easiest method to read any data by passing API URL.

How can I get data from another website in PHP?

In Javascript you can use Ajax to send your request and POSt/GET data. In PHP, you can use [URL=“http://www.php.net/manual/en/book.curl.php”]cURL or the PECL extension [URL=“http://www.php.net/manual/en/book.http.php”]HTTP to send requests and receive responses.

How do you access the data sent through the URL with the GET method in PHP?

The data sent by GET method can be accessed using QUERY_STRING environment variable. The PHP provides $_GET associative array to access all the sent information using GET method.


2 Answers

All the GET variables are put into a superglobal array: $_GET. You can access the value of 'page' with $_GET['page'].

For more information see PHP.Net: $_GET

like image 69
PtPazuzu Avatar answered Oct 21 '22 07:10

PtPazuzu


It's not clear whether you're talking about finding the value of page from within the PHP page handling that URL, or if you have a string containing the URL and you want to parse the page parameter.

If you're talking about accessing query string parameters from within products.php, you can use the super-global $_GET, which is an associative array of all the query string parameters passed to your script:

echo $_GET['page']; // 12

If you're talking about a URL stored in a string, you can parse_url to get the parts of the URL, followed by parse_str to parse the query portion:

$url = "someplace.com/products.php?page=12";
$parts = parse_url($url);
$output = [];
parse_str($parts['query'], $output);
echo $output['page']; // 12
like image 28
meagar Avatar answered Oct 21 '22 07:10

meagar