2017-04-16 10 views
1

質問:どのようにXMLから<media:content URL="IMG" />を解析するのですか?PHP - RSSパーサーXML

OK。これはなぜ1 + 1 = 2であるかを尋ねるようなものです.2 + 2 =利用できません。

オルガナルリンク: SimpleXMLとPHPでXMLを解析する方法// John Morris https://www.youtube.com/watch?v=_1F1Iq1IIS8

私は簡単に次のコードを使用してRSSフィードニューヨーク・タイムズ 上の項目に到達することができ、彼の方法を使用して:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>How to Parse XML with SimpleXML and PHP</title> 
</head> 
<body> 
<?php 
$url = 'http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml'; 
$xml = simplexml_load_file($url) or die("Can't connect to URL"); 

?><pre><?php //print_r($xml); ?></pre><?php 

foreach ($xml->channel->item as $item) { 
    printf('<li><a href="%s">%s</a></li>', $item->link, $item->title); 
} 
?> 
</body> 
</html> 

は与える:記念碑公園で
スパーキー・ライル?ファンはイエスだと言っているが、彼はそれに同意しない。
N.B.Aの背後にある厚いアクセントのアメリカ人。プロバスケットボールで フランス
中:「急いで醜い得たことを」:メディアに到達するためにスパーズ
...

しかし

で配信もっとプレーオフ痛み:それはdoesnのようなコンテンツは、あなたがsimplexml_load_file使用することはできませんmedia.contentタグを取得しないでください。

だから...はい、私はWebbを検索しました。 私はStackOverflowの上でこの例を見つけました:
get media:description and media:content url from xml

が、コードを使用して:

<?php 
function feeds() 
{ 
    $url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data 
    $feeds = file_get_contents($url); 
    $rss = simplexml_load_string($feeds); 
    foreach($rss->channel->item as $entry) { 
     if($entry->children('media', true)->content->attributes()) { 
       $md = $entry->children('media', true)->content->attributes(); 
       print_r("$md->url"); 
      } 
    } 
} 
?> 

は私にエラーを与えませんでした。しかし、空白のページ。

そして、ほとんどの人(グーグルグーグル)は、メディア:コンテンツを実際にどのように使用するかをほとんどまたは全く考えていないようです。だから私はStackoverflowのために回って、誰かが答えを提供できることを願っています。私もSimpleXMLを使いたくない。

私は何をしたいのですか:メディアをつかむ:content url IMAGES、それを外部サイトで使用します。

でも可能です。
XML解析された項目をSQLデータベースに入れたいと思います。

答えて

2

私はこの思い付いた:与える

<?php 
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data 
$feeds = file_get_contents($url); 
$rss = simplexml_load_string($feeds); 

$items = []; 

foreach($rss->channel->item as $entry) { 
    $image = ''; 
    $image = 'N/A'; 
    $description = 'N/A'; 
    foreach ($entry->children('media', true) as $k => $v) { 
     $attributes = $v->attributes(); 

     if ($k == 'content') { 
      if (property_exists($attributes, 'url')) { 
       $image = $attributes->url; 
      } 
     } 
     if ($k == 'description') { 
      $description = $v; 
     } 
    } 

    $items[] = [ 
     'link' => $entry->link, 
     'title' => $entry->title, 
     'image' => $image, 
     'description' => $description, 
    ]; 
} 

print_r($items); 
?> 

Array 
(
    [0] => Array 
     (
      [link] => SimpleXMLElement Object 
       (
        [0] => https://www.nytimes.com/2017/04/17/sports/basketball/a-court-used-for-playing-hoops-since-1893-where-paris.html?partner=rss&emc=rss 
       ) 

      [title] => SimpleXMLElement Object 
       (
        [0] => A Court Used for Playing Hoops Since 1893. Where? Paris. 
       ) 

      [image] => SimpleXMLElement Object 
       (
        [0] => https://static01.nyt.com/images/2017/04/05/sports/basketball/05oldcourt10/05oldcourt10-moth-v13.jpg 
       ) 

      [description] => SimpleXMLElement Object 
       (
        [0] => The Y.M.C.A. in Paris says its basketball court, with its herringbone pattern and loose slats, is the oldest one in the world. It has been continuously functional since the building opened in 1893. 
       ) 

     ) 
..... 

をそして、あなたは

foreach ($items as $item) { 
    printf('<img src="%s">', $item['image']); 
    printf('<a href="%s">%s</a>', $item['url'], $item['title']); 
} 

以上の希望、これは助けを繰り返すことができます。

+0

申し訳ありませんが、私は動揺しました。私は本当に解決策をウェブ上ですべて探しました。 あなたの作品は美しく仕上がります。 ただ 'printfの 'に変更'( '%s'、$項目[ 'URL']、$項目[ 'タイトル']);' 'のprintf( '%s'、$項目[ 'リンク']に' 、$ item ['title']); '' – xxxx

+0

私はどのように到達することができます... サマーセット・パトリオッツのファン・フェストでサインをしているヤンキースとの2度のワールドシリーズチャンピオンを獲得したスパリー・ライル。同じように ? http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml – xxxx

+1

私は自分の答えを更新しました。 @StephanieHallberg – alistaircol