【问题标题】:I'm having trouble passing variables from one php file to another我在将变量从一个 php 文件传递​​到另一个文件时遇到问题
【发布时间】:2015-01-07 19:58:15
【问题描述】:

我正在尝试将变量数据从 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 anotherHow to pass variables from one 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);

?>

【问题讨论】:

  • 要传递什么变量?第一个文件中没有 $feeds!
  • 我已经更新了我的问题。我要传递的变量现在已发布。
  • 您不会在脚本之间“传递变量”。每个脚本在每个 http 请求进入时单独执行,并且任何两个以上的 php 进程之间都没有共享代码。如果您希望一个脚本中的数据显示在另一个脚本中,则必须以某种方式传递它。例如会话、查询参数、表单字段等......
  • @Marc B 以及如何通过会话做到这一点?

标签: php xml json php-parser


【解决方案1】:

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

变量现在应该可用。

【讨论】:

  • 感谢@motanelu 我已经尝试过了,但我还没有得到变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-10
  • 2023-03-17
  • 2022-07-20
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
相关资源
最近更新 更多