我试图将变量数据从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 one page to another 和 How to pass variables from one php page to another without form? 而且我还没有能够使代码正常工作。
我试图传递:$ lbd,$ title,$ guid,$ author,$ description
-Thanks。
这是我的代码:
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);
?>
答案 0 :(得分: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'];
变量现在应该可用。