0

http://feeds.feedburner.com/rb286には、たくさんの画像があります。ただし、simplXmlElement を使用して xml オブジェクトに変換すると、画像が表示されません。私のコード:

if (function_exists("curl_init")){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://feeds.feedburner.com/rb286");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);
curl_close($ch);
//print_r($data);   //here i'm able to see the images
     $doc=new SimpleXmlElement($data);
     print_r($doc);   //here i'm not able to see the images
  }

誰かが xml オブジェクトに変換した後に画像にアクセスする方法を教えてもらえますか? ありがとうございました。

4

1 に答える 1

2

メインタグ<content:encoded>で個々<items>のタグを繰り返し処理する必要があります。タグの選択にはxpath<channel>メソッドを使用します。必要な要素を取得したら、preg_match_allなどの文字列操作ツールを使用してそれらをgrep できます。<img>

編集: feedburner や他の CDN からの広告を除外する、より洗練されたイメージ タグ マッチングを追加しました。

$xml = simplexml_load_string(file_get_contents("http://feeds.feedburner.com/rb286"));

foreach ($xml->xpath('//item/content:encoded') as $desc) {
    preg_match_all('!(?<imgs><img.+?src=[\'"].*?http://feeds.feedburner.com.+?[\'"].+?>)!m', $desc, $>

    foreach ($m['imgs'] as $img) {
        print $img;
    }
}

<content:encoded>タグはネームスペース化されているため、simplexml のビルトイン プロパティ マッピングを使用する場合は、次のように処理する必要があります。

// obtain simplexml object of the feed as before
foreach ($xml->channel->item as $item) {
    $namespaces = $item->getNameSpaces(true);
    $content = $item->children($namespaces['content']);
    print $content->encoded; // use it howevery you want
}

xpath クエリ言語の詳細については、こちらを参照してください。

于 2012-07-23T06:57:59.623 に答える