包含带有json mime类型的php文件,而不更改包含该文件的页面的mime类型

包含带有json mime类型的php文件,而不更改包含该文件的页面的mime类型,php,json,Php,Json,在PHP脚本中,我想获取由另一个PHP脚本动态生成的一些json数据 例如,让我们使用以下动态PHP生成的JSON文件的简单示例: <?php // note: unlike this simplified example, the data in my script is dynamically generated $some_data = array( 'color' => 'blue', 'number' => 33, 'length' =>

在PHP脚本中,我想获取由另一个PHP脚本动态生成的一些json数据

例如,让我们使用以下动态PHP生成的JSON文件的简单示例:

<?php
// note: unlike this simplified example, the data in my script is dynamically generated
$some_data = array(
    'color'  => 'blue',
    'number' => 33,
    'length' => 'long'
);
header('Content-Type: application/json');
echo json_encode($some_data);
现在,我如何从另一个PHP文件中包含这个PHP文件,以便动态获取数据

如果我使用
$data=include'json_data.php',它不将数据存储在我的$data变量中,它采用在该include中定义的mime类型头,并确定页面是该mime类型,而不是从include中确定数据是该mime类型和separate

file\u get\u contents()
fopen()
仅返回实际的PHP代码,而不是呈现它并返回结果json数据

我可以用卷发

<?php
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://www.example.com/json_data.php');
header('Content-Type: application/json');
echo curl_exec($c);
curl_close($c);

为什么不创建一个函数来处理返回JSON(或用于创建JSON的数组)的逻辑,并在两个脚本中使用它,这样就不必包含第一个PHP文件,只需包含共享函数的文件

我的意思是:

<?php 

function getJson() 
{

    // Your real logic, not static content

    return [
        'color'  => 'blue',
        'number' => 33,
        'length' => 'long'
    ];
}

这条逻辑线肯定是另一种解决方案。
<?php 

function getJson() 
{

    // Your real logic, not static content

    return [
        'color'  => 'blue',
        'number' => 33,
        'length' => 'long'
    ];
}