say my URL is:
someplace.com/products.php?page=12
How can I then get the information regarding page being equal to 12?
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With