PHP 将变量发送到 file_get_contents()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6905706/
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
PHP sending variables to file_get_contents()
提问by Richard Westington
I want to be able to send a few variables to a file through file_get_contents()
.
我希望能够通过file_get_contents()
.
This is firstfile.php:
这是 firstfile.php:
<?php
$myvar = 'This is a variable';
// need to send $myvar into secondfile.php
$mystr = file_get_contents('secondfile.php');
?>
This is secondfile.php:
这是secondfile.php:
The value of myvar is: <?php echo $myvar; ?>
I want the variable $mystr
to equal 'The value of myvar is: This is a variable'
我希望变量$mystr
等于'The value of myvar is: This is a variable'
Is there any other function that will let you do this in PHP?
有没有其他函数可以让您在 PHP 中执行此操作?
回答by Nicole
There is a big difference between getting the contents of a fileand running a script:
获取文件内容和运行脚本之间有很大的不同:
include
— this PHP directive runs the specified file as a script, and the scope is the same as the scope where theinclude
call is made. Therefore, to "pass" variables to it, you simply need to define them before callinginclude
.include
can only be used locally(can only include files on the same file system as the PHP server).file_get_contents
— When getting a file locally, this simply retrieves the text that is contained in the file. No PHP processing is done, so there is no way to "pass" variables. If you inspect$myvar
in your example above, you will see that it contains the exact string "<?php echo $myvar; ?>
" — it has not been executed.However, PHP has confused some things a little by allowing
file_get_contents
to pull in the contents of a "remote" file — an internet address. In this case, the concept is the same — PHP just pulls in the raw result of whatever is contained at that address — but PHP, Java, Ruby, or whatever else is running on that remoteserver mayhave executed something to produce that result.In that case, you can "pass" variables in the URL (referred to as
GET
request parameters) according to the specifications of the URL (if it is an API, or something similar). There is no way to pass variables of your choosing that have not been specified to be handled in the script that will be processed by that remote server.Note: The "remote server" referred to MAYbe your own server, though be careful, because that can confuse things even more if you don't really know how it all works (it becomes a second, fully separate request).There is usually not a good reason to do this instead of using
include
, even though they can accomplish similarresults.
include
— 该 PHP 指令将指定文件作为脚本运行,作用域与include
调用的作用域相同。因此,要将变量“传递”给它,您只需在调用include
.include
只能在本地使用(只能包含与 PHP 服务器在同一文件系统上的文件)。file_get_contents
— 在本地获取文件时,这只是检索文件中包含的文本。没有进行 PHP 处理,因此无法“传递”变量。如果您$myvar
在上面的示例中进行检查,您将看到它包含确切的字符串“<?php echo $myvar; ?>
”——它还没有被执行。然而,PHP 通过允许
file_get_contents
拉入“远程”文件的内容——一个互联网地址,让一些事情变得有些混乱。在这种情况下,概念是相同的——PHP 只是提取包含在该地址中的任何内容的原始结果——但是 PHP、Java、Ruby 或在该远程服务器上运行的其他任何东西可能已经执行了某些操作来产生该结果。在这种情况下,您可以
GET
根据 URL 的规范(如果它是 API 或类似的东西)在 URL 中“传递”变量(称为请求参数)。无法传递您选择的变量,这些变量尚未指定在将由该远程服务器处理的脚本中处理。注意:所指的“远程服务器”可能是您自己的服务器,但要小心,因为如果您真的不知道它是如何工作的(它成为第二个完全独立的请求),这可能会使事情更加混乱。通常没有充分的理由来代替使用
include
,即使它们可以实现类似的结果。
回答by Wrikken
ob_start();
include 'secondfile.php';
$myvar = ob_get_clean();
Be advised though: to many ob_start
's in your code are usually a sign you should define functions which return strings, which you can then choose to echo or not.
不过请注意:对于ob_start
代码中的many 's 通常是一个标志,您应该定义返回字符串的函数,然后您可以选择是否回显。
回答by Mozzart
You may use the sprintf function, for example: firstfile.txt
您可以使用 sprintf 函数,例如:firstfile.txt
The value of my var is: %s
secondfile.php
第二个文件
<?php
$f = file_get_contents("firstfile.txt");
$var = "some";
echo sprintf($f, $var);
Result will be
结果将是
The value of my var is: some
我的 var 的值是:一些
回答by lStoilov
I see that this quite an old discussion and my answer may not be quite relevant 7 years later, but just wanted to share how got around the same issue. I just added a dummy text in the secondfile.php (in my case ttt555rrrttt), then I am just replacing that text with the string I want.
我看到这是一个相当古老的讨论,我的答案在 7 年后可能不太相关,但只是想分享如何解决同一问题。我只是在 secondfile.php 中添加了一个虚拟文本(在我的例子中是 ttt555rrrttt),然后我只是用我想要的字符串替换该文本。
$value_to_add = "some value";
$myvar = file_get_contents('secondfile.php');
$myvar = str_replace("ttt555rrrttt", "$value_to_add", $myvar);
That worked for me, and maybe it will work for someone else.
这对我有用,也许对其他人有用。
No idea how good it will be from a performance point of view, but having in mind that I am using it to send an email template to a user, the performance shouldn't be a problem.
不知道从性能的角度来看它会有多好,但请记住,我正在使用它向用户发送电子邮件模板,性能应该不是问题。
回答by Andrey
The following code snippet shows an easy way how to send a HTTP post request using the PHP function file_get_contents:
以下代码片段展示了如何使用 PHP 函数 file_get_contents 发送 HTTP post 请求的简单方法:
<?php
$url = 'http://url.com/path';
$data = array('param1' => 'value1', 'param2' => 'value2')
// use key 'http' even if you send the request to https://...
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
print_r($result);