-1

im trying to get youtube video title from

http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=dpe11FCFxBE&format=xml

in xml title is in

<oembed>
<title>
TITLE IS HERE
</title>
</oembed>

so i try this code to get title but it dont work

$json_output = file_get_contents("http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=".$video_id."&format=xml");
$fln = $json['oembed']['title'];
Ar Megatox
  • 57
  • 1
  • 6
  • @Quentin He has typo in code. – vps May 19 '15 at 07:15
  • How a string stored in `$json_output` become an associative array in `$json` ? Are you missing some code ? – Makesh May 19 '15 at 07:16
  • @Makesh yes he has copied this from somewhere and has typo in it. – vps May 19 '15 at 07:16
  • @VPS : My doubt is, is there any way to parse xml to json ? Thats why i asked has he used any API or method, to do such conversion ? Either he has to choose XML parsing or JSON parsing. Implementation is confusing little bit – Makesh May 19 '15 at 07:20
  • @Makesh he used xml as format but trying json to parse, he just needs title from API call, so i provided him an answer but no reply yet. – vps May 19 '15 at 07:23
  • @vps : Your answer will work .. – Makesh May 19 '15 at 07:23

1 Answers1

1

Try this code...

$content = file_get_contents("http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=".$video_id."&format=json");
$json_output = json_decode($content, true);
$fln = $json_output['title'];

You should use format=json if you want to parse json output.

vps
  • 422
  • 2
  • 6
  • not working $content = file_get_contents("http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=".$video_id."&format=json"); $json_output = json_decode($content, true); $fln = $json_output['title']; – Ar Megatox May 19 '15 at 07:27
  • try `var_dump($content)` and let me know what it outputs; – vps May 19 '15 at 07:29