Getting data from the URL in PHP?

Please check below code to get data from particular URL

<?php
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'YOUR URL');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

$jsonData = json_decode(curl_exec($curlSession));
curl_close($curlSession);
?>

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

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


You can use superglobal variable $_GET. In this case $_GET['page'].

Tags:

Php