PHP 将变量发送到 file_get_contents()

发布于 2024-11-27 15:58:27 字数 448 浏览 0 评论 0原文

我希望能够通过 file_get_contents() 将一些变量发送到文件。

这是firstfile.php:

<?php
$myvar = 'This is a variable';
// need to send $myvar into secondfile.php
$mystr = file_get_contents('secondfile.php');
?>

这是secondfile.php:

The value of myvar is: <?php echo $myvar; ?>

我希望变量$mystr等于'Myvar的值是:这是一个变量'

还有其他吗可以让你在 PHP 中执行此操作的函数吗?

I want to be able to send a few variables to a file through file_get_contents().

This is 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:

The value of myvar is: <?php echo $myvar; ?>

I want the variable $mystr to equal 'The value of myvar is: This is a variable'

Is there any other function that will let you do this in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一曲琵琶半遮面シ 2024-12-04 15:58:27

获取文件的内容运行脚本之间有很大的区别:

  • include — 此 PHP 指令作为脚本运行指定文件,并且范围与进行 include 调用的范围相同。因此,要将变量“传递”给它,您只需在调用 include 之前定义它们。 include 只能在本地使用(只能包含与 PHP 服务器位于同一文件系统上的文件)。

  • file_get_contents — 在本地获取文件时,这只是检索文件中包含的文本。没有进行 PHP 处理,因此无法“传递”变量。如果您检查上面示例中的 $myvar,您将看到它包含确切的字符串“” — 它没有已被处决。

    但是,PHP 通过允许 file_get_contents 提取“远程”文件(互联网地址)的内容,使一些事情变得有点混乱。在这种情况下,概念是相同的 - PHP 只是提取该地址中包含的任何内容的原始结果 - 但 PHP、Java、Ruby 或该远程服务器上运行的任何其他内容可能已经执行了某些操作来产生该结果。

    在这种情况下,您可以根据 URL 的规范(如果是 API 或类似的东西)在 URL 中“传递”变量(称为 GET 请求参数)。无法传递您选择的尚未在将由该远程服务器处理的脚本中指定处理的变量。

    注意:所指的“远程服务器”可能是您自己的服务器,但要小心,因为如果您真的不知道它是如何工作的,这可能会让事情变得更加混乱(它成为第二个完全独立的请求)。通常没有充分的理由这样做而不是使用include,即使它们可以完成类似结果。

There is a big difference between getting the contents of a file and running a script:

  • include — this PHP directive runs the specified file as a script, and the scope is the same as the scope where the include call is made. Therefore, to "pass" variables to it, you simply need to define them before calling include. 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 remote server may have 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 MAY be 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 similar results.

三人与歌 2024-12-04 15:58:27
ob_start();
include 'secondfile.php';
$myvar = ob_get_clean();

但请注意:代码中的许多 ob_start 通常是一个标志,您应该定义返回字符串的函数,然后您可以选择是否回显这些字符串。

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.

梦在深巷 2024-12-04 15:58:27

我发现这是一个相当古老的讨论,我的回答可能在 7 年后不太相关,但只是想分享如何解决同样的问题。
我刚刚在 secondaryfile.php 中添加了一个虚拟文本(在我的例子中是 ttt555rrrttt),然后我只是用我想要的字符串替换该文本。

$value_to_add = "some value";

$myvar = file_get_contents('secondfile.php');
$myvar = str_replace("ttt555rrrttt", "$value_to_add", $myvar);

这对我有用,也许对其他人也有用。

从性能的角度来看,不知道它会有多好,但考虑到我正在使用它向用户发送电子邮件模板,性能应该不成问题。

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.

$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.

一梦等七年七年为一梦 2024-12-04 15:58:27

您可以使用 sprintf 函数,例如:
firstfile.txt

The value of my var is: %s

secondaryfile.php

<?php
$f = file_get_contents("firstfile.txt");
$var = "some";
echo sprintf($f, $var);

结果将是

我的 var 的值为:some

You may use the sprintf function, for example:
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

身边 2024-12-04 15:58:27

以下代码片段显示了如何使用 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);

OR 在 URL 中传递变量 (php) & file_get_contents()

The following code snippet shows an easy way how to send a HTTP post request using the PHP function file_get_contents:

<?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);

OR Passing variable in a URL (php) & file_get_contents()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文