0

我正在尝试将变量数据从 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 - Passing variables from a page to another and How to pass variables from a php page to another without form? 而且我无法使代码正常工作。

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

-谢谢。

这是我的代码:

php1.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;
?>

php2.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);

?>
4

1 回答 1

0

添加 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'];

变量现在应该可用。

于 2015-01-07T20:23:22.063 回答