0

I'm trying to parse out some data from an MRSS feed using SimpleXML then create JSON with that data.

Here's what I'm trying, it does output JSON but the formatting looks bad and given all the [{"0":" before every element, I'm sure I'm doing something wrong:

<?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);
}

?>

I've tried to both

$json_string = json_encode($arr, 128)

and

$json_string = json_encode($arr, JSON_PRETTY_PRINT)

"128" seems to print out better than "JSON_PRETTY_PRINT"

Any ideas? I'm sure it's obvious, but I'm a newbie with PHP. Thanks!

  • 2
    `JSON_PRETTY_PRINT` equals `128`. Maybe you could concretise your stylistic observations. json_encode()ing each element in the loop is probably not what you want. – mario Oct 21 '14 at 19:16
  • @mario thanks for the feedback, not quite sure what you mean by "concretise your stylistic observations" — can you expand on that? Thanks! –  Oct 21 '14 at 20:05

1 Answers1

1

You need to cast the elements as strings. If you don't cast them as strings they are SimpleXMLElements. If you do var_dump($title); you would get back object(SimpleXMLElement)[6] public 0 => string 'Weekend Top 5: Talladega' (length=24) which is why you were getting the [{"0":" before every element.

<?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);
}

?>
slapyo
  • 2,979
  • 1
  • 15
  • 24
  • thanks for the feedback, much appreciated. I just copy and pasted your version of the script above and now I'm getting: Warning: json_encode() expects parameter 2 to be long, string given in /home/stranger/www/ingestdev/LiveAPIUIMethod/JSONCreateScript_01.php on line 19 Warning: json_encode() expects parameter 2 to be long, string given in /home/stranger/www/ingestdev/LiveAPIUIMethod/JSONCreateScript_01.php on line 21 –  Oct 21 '14 at 19:58
  • You can change that to 128 and see how that works for you. You said there was a difference between 128 and JSON_PRETTY_PRINT though there shouldn't be or just leave that off if you don't need it. – slapyo Oct 21 '14 at 21:29
  • 1
    JSON_PRETTY_PRINT requires PHP 5.4.0. Seeing the string warning means the constant is undefined and is replaced with the string containing the constant name. Ergo you're using the wrong PHP version. Most likely the problem is not the pretty encoding but just casting to string. Next to the duplicate close, please see [Pretty-Printing JSON with PHP](http://stackoverflow.com/q/6054033/367456) for reference on site and keep in mind that Stackoverflow works best with asking one question at a time. There is also [the X/Y problem](http://meta.stackexchange.com/q/66377/147909) – hakre Oct 25 '14 at 09:44