从 PHP 中的 URL 获取数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6902128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:34:31  来源:igfitidea点击:

Getting data from the URL in PHP?

php

提问by jmasterx

say my URL is:

说我的网址是:

someplace.com/products.php?page=12

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

我怎样才能获得有关页面等于 12 的信息?

回答by PtPazuzu

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

所有GET变量都放入全局数组:$_GET。您可以访问 'page' 的值$_GET['page']

For more information see PHP.Net: $_GET

有关更多信息,请参阅PHP.Net:$_GET

回答by meagar

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

不清楚您是在page处理该 URL 的 PHP 页面中查找from的值,还是您有一个包含 URL 的字符串并且您想要解析该page参数。

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:

如果您要从 内部访问查询字符串参数products.php,则可以使用 super-global $_GET,它是传递给脚本的所有查询字符串参数的关联数组:

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

If you're talking about a URL stored in a string, you can parse_urlto get the parts of the URL, followed by parse_strto parse the queryportion:

如果您正在谈论存储在字符串中的 URL,您可以parse_url获取 URL 的部分,然后parse_str解析该query部分:

$url = "someplace.com/products.php?page=12";
$parts = parse_url($url);
$output = [];
parse_str($parts['query'], $output);
echo $output['page']; // 12

回答by Santosh

Please check below code to get data from particular URL

请检查以下代码以从特定 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);
?>

回答by Ondrej Slinták

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

您可以使用超全局变量$_GET。在这种情况下$_GET['page']

回答by Manuel

Thats done with the variable $_GET['page'](this gives you the value of page, in this case 12)

这就是用变量完成的$_GET['page'](这为您提供了页面的值,在本例中为 12)

回答by ngen

$_GET['page']

$_GET['page']

You have to use $_GET to grab query info.

您必须使用 $_GET 来获取查询信息。

回答by Santosh

Use $_GET['page'] OR $_REQUEST['page']

使用 $_GET['page'] 或 $_REQUEST['page']