如何在php脚本中连接两个url地址?

时间:2016-08-03 07:45:27

标签: php

我试图在php中创建RSS提要,我想从两个不同的链接中获取数据。澄清;我想从两个不同的网站获取数据,并将其显示为链接为我自己的链接。

现在我从两个不同的网站获取数据,但我无法同时显示两组数据。

这是我的代码,有人可以帮忙吗?

从网址获取q参数$ q = $ _ GET [" q"];

//find out which feed was selected 
if($q==" Tech") {  
 $xml=("http://----/myrssfeed.xml"); 
} elseif($q=="Herald") {  
 $xml=("http://******/top-stories.rss"); }
 $xmlDoc = new DOMDocument(); $xmlDoc->load($xml);


 //get elements from "<channel>"
 $channel=$xmlDoc->getElementsByTagName('channel')->item(0);
 $channel_title = $channel->getElementsByTagName('title')

 ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link')

 ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description')

 ->item(0)->childNodes->item(0)->nodeValue;


 //output elements from "<channel>"
 echo("<p><a href='" . $channel_link. "'>" . $channel_title . "</a>"); 
 echo("<br>");
 echo($channel_desc . "</p>");

 //get and output "<item>" elements
 $x=$xmlDoc->getElementsByTagName('item'); 

for ($i=0; $i<=11; $i++) {  
 $item_title=$x->item($i)->getElementsByTagName('title')  

 ->item(0)->childNodes->item(0)->nodeValue;   $item_link=$x->item($i)->getElementsByTagName('link')  
 ->item(0)->childNodes->item(0)->nodeValue;   $item_desc=$x->item($i)->getElementsByTagName('description')  
 ->item(0)->childNodes->item(0)->nodeValue;   

 echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>");  
 echo ("<br>");   
 echo($item_desc . "</p>");
} ?>

1 个答案:

答案 0 :(得分:0)

        <?php
        $q=$_GET["q"];
        $xml1=("http://----/myrssfeed.xml"); 
        $xml2=("http://----/myrssfeed.xml"); 
        $xmlDoc1 = new DOMDocument(); $xmlDoc->load($xml1);
        $xmlDoc2 = new DOMDocument(); $xmlDoc->load($xml2);

         $channel=$xmlDoc1->getElementsByTagName('channel')->item(0); $channel_title = $channel->getElementsByTagName('title')

        ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link')

        ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description')

        ->item(0)->childNodes->item(0)->nodeValue;
         echo("

        " . $channel_title . ""); echo("
        "); echo($channel_desc . "

        ");

        $x=$xmlDoc1->getElementsByTagName('item'); for ($i=0; $i<=11; $i++) {
        $item_title=$x->item($i)->getElementsByTagName('title')

        ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link')

        ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description')

        ->item(0)->childNodes->item(0)->nodeValue; echo ("

        " . $item_title . ""); echo ("
        "); echo ($item_desc . "

        "); }


        $channel=$xmlDoc1->getElementsByTagName('channel')->item(0); $channel_title = $channel->getElementsByTagName('title')

        ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link')

        ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description')

        ->item(0)->childNodes->item(0)->nodeValue;
         echo("

        " . $channel_title . ""); echo("
        "); echo($channel_desc . "

        ");

        $x1=$xmlDoc2->getElementsByTagName('item'); for ($j=0; $j<=11; $j++) {
        $item_title1=$x1->item($j)->getElementsByTagName('title')

        ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x1->item($j)->getElementsByTagName('link')

        ->item(0)->childNodes->item(0)->nodeValue; $item_desc1=$x1->item($j)->getElementsByTagName('description')

        ->item(0)->childNodes->item(0)->nodeValue; echo ("

        " . $item_title1 . ""); echo ("
        "); echo ($item_desc1 . "

        "); }


        $channel1=$xmlDoc2->getElementsByTagName('channel')->item(0); $channel_title1 = $channel1->getElementsByTagName('title')

        ->item(0)->childNodes->item(0)->nodeValue; $channel_link1 = $channel1->getElementsByTagName('link')

        ->item(0)->childNodes->item(0)->nodeValue; $channel_desc1 = $channel1->getElementsByTagName('description')

        ->item(0)->childNodes->item(0)->nodeValue;
         echo("

        " . $channel_title1 . ""); echo("
        "); echo($channel_desc1 . "

        ");

         ?>

制作2个不同的url和xml解析器变量并使用