如何执行并获取变量中 .php 文件的内容?
-
21-08-2019 - |
题
我想获取其他页面上变量中的 .php 文件的内容。
我有两个文件, myfile1.php
和 myfile2.php
.
myfile2.php
<?PHP
$myvar="prashant"; //
echo $myvar;
?>
现在我想在 myfile1.php 的变量中获取 myfile2.php 回显的值,我尝试了以下方法,但它也获取了包括 php tag () 在内的所有内容。
<?PHP
$root_var .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/myfile2.php", true);
?>
请告诉我如何将一个 PHP 文件返回的内容放入另一个 PHP 文件中定义的变量中。
谢谢
解决方案
您可以使用包括指令来做到这一点。
文件2:
<?php
$myvar="prashant";
?>
文件1:
<?php
include('myfile2.php');
echo $myvar;
?>
其他提示
你必须区分两件事:
- 你想捕获输出(
echo
,print
,...) 的包含文件并在变量(字符串)中使用输出? - 您是否想从包含的文件中返回某些值并将它们用作您的变量 主持人 脚本?
包含文件中的局部变量将始终移动到您的当前范围 主持人 脚本 - 应该注意这一点。您可以将所有这些功能合并为一个:
include.php
$hello = "Hello";
echo "Hello World";
return "World";
host.php
ob_start();
$return = include 'include.php'; // (string)"World"
$output = ob_get_clean(); // (string)"Hello World"
// $hello has been moved to the current scope
echo $hello . ' ' . $return; // echos "Hello World"
这 return
-feature 非常有用,尤其是在使用配置文件时。
config.php
return array(
'host' => 'localhost',
....
);
app.php
$config = include 'config.php'; // $config is an array
编辑
为了回答您有关使用输出缓冲区时性能损失的问题,我只是做了一些快速测试。1,000,000 次迭代 ob_start()
以及相应的 $o = ob_get_clean()
在我的 Windows 机器上大约需要 7.5 秒(可以说不是 PHP 的最佳环境)。我想说,性能影响应该被认为是相当小的......
如果您只是想通过包含页面echo()
'ed的内容,你可以考虑使用输出缓冲:
ob_start();
include 'myfile2.php';
$echoed_content = ob_get_clean(); // gets content, discards buffer
我总是试图避免的 ob_
强>功能。相反,我使用:
<?php
$file = file_get_contents('/path/to/file.php');
$content = eval("?>$file");
echo $content;
?>
“其实我只是在寻找的是有没有返回类型的方法,可以直接给我的价值” - 你刚才已经回答你自己的问题。
。请参阅 http://sg.php.net/manual/en/function .include.php ,实施例#5
file1.php:
<? return 'somevalue'; ?>
file2.php:
<?
$file1 = include 'file1.php';
echo $file1; // This outputs 'somevalue'.
?>
您可以使用输出缓冲器,将保存所有你输出,并不会打印出来,除非你明确告诉它,或者不结/被执行的路径结束清除缓冲区。
// Create an output buffer which will take in everything written to
// stdout(i.e. everything you `echo`ed or `print`ed)
ob_start()
// Go to the file
require_once 'file.php';
// Get what was in the file
$output = ob_get_clean();
如果你想通过
让各地现场使用<?php
$URL = 'http://www.example.com/';
$homepage = file_get_contents($URL);
echo $homepage;
?>
请尝试此代码
myfile1.php
<?php
echo file_get_contents("http://domainname/myfile2.php");
?>
myfile2.php
<?PHP
$myvar="prashant";
echo $myvar;
?>
如果您想返回从代码的输出在文件中,只是简单地做一个RESTful API调用它。这样,您就可以使用AJAX调用,REST API相同的代码文件,或为内部的PHP代码。
它需要卷曲被安装,但没有输出缓冲器或不包括刚刚执行的页面,并返回成一个字符串。
我给你我写的代码。它适用于几乎所有的REST / Web服务器(甚至与Equifax公司工作):
$return = PostRestApi($url);
或
$post = array('name' => 'Bob', 'id' => '12345');
$return = PostRestApi($url, $post, false, 6, false);
下面是函数:
/**
* Calls a REST API and returns the result
*
* $loginRequest = json_encode(array("Code" => "somecode", "SecretKey" => "somekey"));
* $result = CallRestApi("https://server.com/api/login", $loginRequest);
*
* @param string $url The URL for the request
* @param array/string $data Input data to send to server; If array, use key/value pairs and if string use urlencode() for text values)
* @param array $header_array Simple array of strings (i.e. array('Content-Type: application/json');
* @param int $ssl_type Set preferred TLS/SSL version; Default is TLSv1.2
* @param boolean $verify_ssl Whether to verify the SSL certificate or not
* @param boolean $timeout_seconds Timeout in seconds; if zero then never time out
* @return string Returned results
*/
function PostRestApi($url, $data = false, $header_array = false,
$ssl_type = 6, $verify_ssl = true, $timeout_seconds = false) {
// If cURL is not installed...
if (! function_exists('curl_init')) {
// Log and show the error
$error = 'Function ' . __FUNCTION__ . ' Error: cURL is not installed.';
error_log($error, 0);
die($error);
} else {
// Initialize the cURL session
$curl = curl_init($url);
// Set the POST data
$send = '';
if ($data !== false) {
if (is_array($data)) {
$send = http_build_query($data);
} else {
$send = $data;
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $send);
}
// Set the default header information
$header = array('Content-Length: ' . strlen($send));
if (is_array($header_array) && count($header_array) > 0) {
$header = array_merge($header, $header_array);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
// Set preferred TLS/SSL version
curl_setopt($curl, CURLOPT_SSLVERSION, $ssl_type);
// Verify the server's security certificate?
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, ($verify_ssl) ? 1 : 0);
// Set the time out in seconds
curl_setopt($curl, CURLOPT_TIMEOUT, ($timeout_seconds) ? $timeout_seconds : 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$result = curl_exec($curl);
// Close cURL resource, and free up system resources
curl_close($curl);
unset($curl);
// Return the results
return $result;
}
}