Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用simplexml PHP脚本的MRSS提要中的命名空间_Php_Xml_Parsing_Simplexml - Fatal编程技术网

使用simplexml PHP脚本的MRSS提要中的命名空间

使用simplexml PHP脚本的MRSS提要中的命名空间,php,xml,parsing,simplexml,Php,Xml,Parsing,Simplexml,我试图调查我在这里做错了什么,但到目前为止运气不好。我想使用这个脚本拉取这个MRSS提要中的链接和URL,但它不起作用。我认为我所需要做的就是使用名称空间来获取子元素,但运气不好: <?php $html = ""; $url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_s

我试图调查我在这里做错了什么,但到目前为止运气不好。我想使用这个脚本拉取这个MRSS提要中的链接和URL,但它不起作用。我认为我所需要做的就是使用名称空间来获取子元素,但运气不好:

<?php
$html = "";
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces

for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos 
  $title = $xml->channel->item[$i]->title;
  $link = $xml->channel->item[$i]->link;
  $pubDate = $xml->channel->item[$i]->pubDate;
  $description = $xml->channel->item[$i]->description;
  $titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
  $url = $xml->channel->item[$i]->children($namespaces['media'])->url;
  $html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p>
    <iframe width='480' height='270' src='http://link.brightcove.com/services/player/bcpid3742068445001?bckey=//my API token goes here &bctid=$titleid&autoStart=false' frameborder='0'></iframe><hr/>";/* this embed code is from the youtube iframe embed code format but is actually using the embedded Ooyala player embedded on the Campus Insiders page. I replaced any specific guid (aka video ID) numbers with the "$guid" variable while keeping the Campus Insider Ooyala publisher ID, "eb3......fad" */
}
echo $html;
?>
我做错了什么

谢谢你的帮助

  • 医学博士

SimpleXML的名称有欺骗性,因为它比DOMDocument或其他PHP XML扩展更难使用。要获取URL,您需要访问
media:content
节点的
URL
属性:

<media:content duration="95" medium="video" type="video/mp4"
 url="http://brightcove.meta.nascar.com.edgesuite.net/vod/etc"/>
并获取其属性:

$m_attrs = 
  $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
然后,您可以访问
url
属性:

echo "URL: " . $m_attrs["url"] . "\n";
因此,您的代码应该是:

$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$url = $m_attrs["url"];

$html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p> (etc.)";
$titleid=$xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs=$xml->channel->item[$i]->children($namespace['media'])->content[0]->attributes();
$url=$m_attrs[“url”];
$html.=“$title$description$pubDate$link视频ID:$titleid(等)”;
echo "URL: " . $m_attrs["url"] . "\n";
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$url = $m_attrs["url"];

$html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p> (etc.)";