2

この xml ファイルを解析するために simpleXML を使用しています。YouTube API にアクセスするために使用しているフィードです。最新のビデオをオブジェクトに埋め込み、次の 4 つのサムネイルを表示したいと考えています。

これを使用simplexml_load_fileして、foreachループを使用して各フィードの値にアクセスします。

値に問題なくアクセスできますが、各ビデオを個別の に保存するという問題が発生しSimpleXMLElement Objectます。オブジェクトは配列に格納されていないため、アクセスしているオブジェクトを制御することはできません。だから私は得ることができない、言う、$thumb[4]または$entry[4]->thumb.

を使用しようとしましSimpleXMLIteratorたが、何らかの理由で、先頭が同じ値はすべて空白としてレンダリングされます。たとえば、ビデオには次の 11 のバリエーションがあります。

そして、これらはそれぞれ 、 、 などとしてレンダリングさ[1]=""[2]=""ます[3]=""

助けていただける方に、喜んでさらに情報を提供します!

編集

これが私の結果のprint_rです。これは、私が直面している構造の問題を理解するために、単一の変数に対して行われます。print_r($entry) 全体は、XML ファイル内の各ノードの変数を提供します。

SimpleXMLElement Object
(
    [0] => 159
)
SimpleXMLElement Object
(
    [0] => 44
)

また、print_r は単純にテスト用の PHP ブロック内にあります。私は実際にHTMLのエコー内の変数にアクセスしようとしています。

4

4 に答える 4

7

名前空間と SimpleXMLの問題に遭遇しました。フィードは次で始まります

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>

xmlns='http://www.w3.org/2005/Atom'、デフォルトの名前空間を に設定しますhttp://www.w3.org/2005/Atom。つまり、その子要素は実際idには ,updatedではcategoryなくhttp://www.w3.org/2005/Atom:id,http://www.w3.org/2005/Atom:updatedなどです...
したがって、 を介して id 要素にアクセスすることはできません。メソッドSimpleXMLELement::children()$feed->idが必要です。取得する子要素の名前空間を指定できます。

例えば、

$feed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true');
$children = $feed->children('http://www.w3.org/2005/Atom');
echo $children->updated;

現在印刷され2010-02-06T05:23:33.858Zます。

最初のエントリ要素の ID を取得するには、 を使用できますecho $children->entry[0]->id;。しかし、その後、名前空間にあるelement とその children などにヒット
します。<media:group><media:category<media:player>xmlns:media='http://search.yahoo.com/mrss/'

$feed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true');
$group = $feed->children('http://www.w3.org/2005/Atom')
  ->entry[0]
  ->children('http://search.yahoo.com/mrss/')
  ->group
  ->children('http://search.yahoo.com/mrss/')
;
echo $group->player->attributes()->url, "\n";
foreach( $group->thumbnail as $thumb) {
  echo 'thumb: ', $thumb->attributes()->url, "\n";
}

(現在) 版画

http://www.youtube.com/watch?v=ikACkCpJ-js&feature=youtube_gdata
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/2.jpg
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/1.jpg
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/3.jpg
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/0.jpg

編集: 私はおそらく、より多くの JavaScript を使用してブラウザー内でそれを行うでしょうが、ここに (単純で醜い) サンプルアプリを示します。

<?php
//define('TESTENV' , true);
function getFeed($author) {
  // <-- add caching here if needed -->
  if ( !defined('TESTENV') ) {
    $url = sprintf('http://gdata.youtube.com/feeds/api/videos?author=%s&max-results=5',
      urlencode($author)
    );
  }
  else {
    $url = 'feed.xml';
  }
  return simplexml_load_file($url);
}
$feed = getFeed('jonlajoie');

if ( !isset($_GET['id']) ) {
  $selected = '';
}
else {
  $selected =  $_GET['id'];
  printf ('
    <object width="425" height="344">
      <param name="movie" value="http://www.youtube.com/v/%s"></param>
      <param name="allowFullScreen" value="true"></param>
      <param name="allowscriptaccess" value="always">
      </param>
      <embed src="http://www.youtube.com/v/%s" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
    </object>',
    htmlspcialchars($selected), htmlspcialchars($selected)
  );
}

$item = 0;
foreach( $feed->children('http://www.w3.org/2005/Atom')->entry as $entry ) {
  $entryElements = $entry->children('http://www.w3.org/2005/Atom');
  $groupElements = $entry
    ->children('http://search.yahoo.com/mrss/')
    ->group
    ->children('http://search.yahoo.com/mrss/')
  ;

  if ( !preg_match('!^http://gdata.youtube.com/feeds/api/videos/([^/]+)!', $entryElements->id, $m) ) {
    // google can choose whatever id they want. But this is only a simple example....
    die('unexpected id: '.htmlspecialchars($entryElements->id));
  }
  $id = $m[1];
  if ( $selected!==$id ) {
    printf('<a href="?id=%s"><img src="%s" /></a>',
      urlencode($id),
      $groupElements->thumbnail[0]->attributes()->url
    );
  }
}

編集 2: 「サムネイルをクリックすると、jQuery を使用してビデオをメイン スペースにロードします。ノード [#] への正確なアクセスが必要になるようですよね?」
すでに JavaScript/jQuery を使用している場合、PHP スクリプト (必要な場合) は、すべてのビデオのすべてのデータの (JSON エンコードされた) 配列を返すだけで、jQuery スクリプトはデータの処理方法を理解できます。

于 2010-02-06T06:11:43.220 に答える
2
$doc = new DOMDocument();
$doc->loadXML(file_get_contents('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true'));
$xpd = new DOMXPath($doc);
$xpd->registerNamespace('atom', "http://www.w3.org/2005/Atom");
false&&$node = new DOMElement();//this is for my IDE to have intellysense

$result = $xpd->query('//atom:feed/atom:entry[1]/media:group/media:thumbnail');
foreach($result as $node){
    echo $node->getAttribute('url').'<br />';
}

echo "more results <br />";
$result = $xpd->query('//atom:feed/atom:entry[1]/media:group/media:thumbnail[1]');
foreach($result as $node){
    echo $node->getAttribute('url').'<br />';
}
于 2010-02-06T06:39:36.773 に答える
1

その点で試してみてください$sxml->entry[4]->thumb

別名、最初のエントリ ID にアクセスするには、$sxml->entry[4]->id

基本的$sxml->entryには s の配列ですSimpleXMLElement Object。それらを foreach するとき、各オブジェクトを調べて$entry、それを に割り当て$entry = $sxml->entry[0]ます。

あなたはこのように行くことができます:

print_r($sxml);

全体の構造を表示します。そして、それはあなたがそれにアクセスするために何を使う必要があるかを教えてくれます.

于 2010-02-06T04:39:16.140 に答える
0

最後に、2 つの foreach ループを実行しました。1 つは最新の1 つのエントリを表示する XML フィードを実行します。次に、この周りのオブジェクトをエコーアウトしてビデオを再生しました。

もう 1 つは、最新の 4 つのエントリを調べて、サムネイルのリストのリスト要素を返しました。

これで、サムネイル ビデオをクリックすると、javascript がパラメーターをメイン エントリに渡す処理を行います。さて、パラメータを受け取ったときにメイン オブジェクトをリロードする方法については、自由に考えてください。

于 2010-02-07T06:46:06.003 に答える