我在将变量从一个 php 文件传递到另一个 php 文件时遇到问题


I'm having trouble passing variables from one php file to another

我正在尝试将变量数据从php1.php传递到php2.php。这就是我要做的:

我正在使用 php 从 url 加载一个 xml(未用 feedvalidator.org 验证),然后将变量数据传递给另一个 php2.php(这个创建一个 json 文件)。我没有使用 php 函数将 xml 转换为 json,因为我正在更改结构。这两个文件单独工作得很好:php1.php正确加载xml和php2.php创建了我想要的json结构,我的问题是:我无法将变量数据从php1.php传递给php2.php。我已经尝试过这个:PHP - 将变量从一个页面传递到另一个页面和如何在没有表单的情况下将变量从一个 php 页面传递到另一个页面?而且我一直无法让代码工作。

我正在尝试通过:$lbd,$title,$guid,$author,$description

-谢谢。

这是我的代码:

菲律宾比索1.php

$html = "";
$url = "http://www.conciencia.net/rss.aspx";
$xml = simplexml_load_file($url);
$channel_title = $xml->channel->title;
$channel_link = $xml->channel->link;
$managingEditor = $xml->channel->managingEditor;
$channel_description = $xml->channel->description;
$lbd = $xml->channel->lastBuildDate;
$html .= "<br/>$channel_title";
$html .= "<br/>$channel_link";
$html .= "<br/>$managingEditor";
$html .= "<br/>$lbd";
$html .= "<br/>$channel_description";
for($i = 0; $i < 7; $i++){
    $pubDate = $xml->channel->item[$i]->pubDate;
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
    $guid = $xml->channel->item[$i]->guid;
    $author = $xml->channel->item[$i]->author;
$description = $xml->channel->item[$i]->description;

    $html .= "<br/>$pubDate";     
$html .= "<a href='$link'><h3>$title</h3></a>";
    $html .= "$link";
    $html .= "<br/>$guid";
    $html .= "<br/>$author";
$html .= "<br/>$description";

}
echo $html;
?>

菲律宾比索2.php

$feeds['conciencia'] = array(
 'channel' => array(
     'title' =>"Un Mensaje a la Conciencia", 'link' =>"www.conciencia.net", 'managingEditor'  =>"Hermano Pablo y Carlos Rey", 'description' => "Populares programas de 4 minutos que comienzan     con una anécdota o historia y terminan con una aplicación moral y espiritual. Se han transmitido de lunes a sábado durante más de 40 años. Actualmente se difunden más de 4 mil veces al día en 30 países en la radio, la televisión y la prensa, y ahora via Internet en Conciencia.net.", 'lastBuildDate' => "$lbd", 'language' => "es-ES", 'copyright' => "'u00a9 2015 Asociaci'u00f3n Hermano Pablo",    'item' => array(  
            array(  
                'title' => "$title", 'link' => "$link", 'guid' => "$guid", 'author' => "$author", 'description' => "$description", 'enclosure' => array(
            array( '@attributes' => array( 'url' =>"$url", 'length' => "$length", 'type' => "$type")
            )
            ) 
            )
        )
)
);
echo json_encode($feeds);
?>

在每个文件的开头添加 session_start(); 在回显任何内容之前。然后在文件 1 的末尾,添加以下内容:

$_SESSION['lbd'] = $lbd;
$_SESSION['title'] = $title;
$_SESSION['guid'] = $guid;
$_SESSION['author'] = $author;
$_SESSION['description'] = $description;

然后,在第二个中,添加:

$lbd = $_SESSION['lbd'];
$title = $_SESSION['title'];
$guid = $_SESSION['guid'];
$author = $_SESSION['author'];
$description = $_SESSION['description'];

变量现在应该可用。