我正在尝试使用SimpleXML从MRSS提要中解析出一些数据,然后使用该数据创建JSON。
这是我正在尝试的,它确实输出了JSON,但格式看起来很糟糕并给出了所有[{“0”:“在每个元素之前,我确定我做错了什么:
<?php
session_start();
$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]->video;
$link = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$pubDate = $xml->channel->item[$i]->pubDate;
$description = $xml->channel->item[$i]->description;
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$VideoFileURL = $m_attrs["url"];
$arr = array($title, $description, $VideoFileURL);
$json_string = json_encode($arr, 128);
echo json_encode($arr, 128);
}
?>
我试过两个
$json_string = json_encode($arr, 128)
和
$json_string = json_encode($arr, JSON_PRETTY_PRINT)
“128”似乎比“JSON_PRETTY_PRINT”
打印得更好有什么想法吗?我确定这很明显,但我是PHP的新手。谢谢!
答案 0 :(得分:1)
您需要将元素转换为字符串。如果你不把它们作为字符串转换它们就是SimpleXMLElements。如果您执行var_dump($title);
,则会返回object(SimpleXMLElement)[6]
public 0 => string 'Weekend Top 5: Talladega' (length=24)
,这就是您在每个元素之前获得[{"0":"
的原因。
<?php
session_start();
$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 = (string)$xml->channel->item[$i]->video;
$link = (string)$xml->channel->item[$i]->link;
$title = (string)$xml->channel->item[$i]->title;
$pubDate = (string)$xml->channel->item[$i]->pubDate;
$description = (string)$xml->channel->item[$i]->description;
$titleid = (string)$xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$VideoFileURL = (string)$m_attrs["url"];
$arr = array('title' => $title, 'description' => $description, 'VideoFileURL' => $VideoFileURL);
$json_string = json_encode($arr, JSON_PRETTY_PRINT);
echo json_encode($arr, JSON_PRETTY_PRINT);
}
?>