[英]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
-謝謝。
這是我的代碼:
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);
?>
添加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'];
變量現在應該可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.