$var1 = $_REQUEST['id'];
$var2 = file_get_contents('http://example.com/script.php?z=$var1');
echo $var2;
似乎file_get_contents已被禁用。
我可以用什么替换file_get_contents来实现这个目的?
答案 0 :(得分:3)
您正在使用单引号,这意味着$var1
未被替换为字符串。但即使您使用双引号,您的查询字符串仍可能无法正确转义。
您应该使用http_build_query
来确保构建有效的网址:
$url = 'http://example.com/script.php?' . http_build_query(array(
'z' => $_REQUEST['id']
));
正如Patrick的回答所指出的,如果file_get_contents被禁用并发出警告,您可以启用它。如果没有,您可以尝试使用cURL来发出请求,如下所示:
$url = 'http://example.com/script.php?' . http_build_query(array(
'z' => $_REQUEST['id']
));
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
if ($result === false) {
trigger_error(curl_error($ch));
} else {
// do something with $result
}
curl_close($ch);
答案 1 :(得分:2)
如果您收到类似于以下内容的错误消息:
警告:file_get_contents()[function.file-get-contents]:在...中的服务器配置中禁用了URL文件访问权限。
制作一个只包含以下行的php.ini文件:
allow_url_fopen = On
答案 2 :(得分:1)
你需要使用双引号:
$var2 = file_get_contents("http://example.com/script.php?z=$var1");
答案 3 :(得分:0)
这应该可以工作但是我很确定你在使用单引号时不能在字符串中使用变量替换(PHP的名字吗?)。尝试使用双引号或类似'http://example.com/script.php?z=' . $var1
之类的内容。请告诉我这是否适合您。