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

170 Views Asked by At

I'm trying to pass the variables data from php1.php to php2.php. this is what I'm trying to do:

I'm using php to load a xml(not validated with feedvalidator.org) from a url, and then to pass the variables data to another php2.php(this one creates a json file). I'm not using the php function to convert xml to json because I'm changing the structure. Both files work great separately : php1.php load correctly the xml and php2.php creates the json structure i want, my problem is : i can't pass the variables data from php1.php to php2.php. I've already tried this: PHP - Passing variables from one page to another and How to pass variables from one php page to another without form? and i haven't been able to get the code working.

I'm trying to pass : $lbd, $title, $guid, $author, $description

-thanks.

this is my code :

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

?>
1

There are 1 best solutions below

1
On BEST ANSWER

Add session_start(); at the beginning of each file, before echoing anything. Then at the end of file 1, add this:

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

and then, in the second one, add:

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

The variables should be available now.