2077

Error in Getting Youtube Video Title, Description and thumbnail

Question:

I was getting youtube title and youtube description form the same code but now its not working I am getting following error:

Warning: DOMDocument::load() [domdocument.load]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

Warning: DOMDocument::load(http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY) [domdocument.load]: failed to open stream: no suitable wrapper could be found in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY" in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

.................................... Following Coding is used to get Youtube Video Data:

$url = "http://gdata.youtube.com/feeds/api/videos/".$embedCodeParts2[0]; $doc = new DOMDocument; @$doc->load($url); $title = $doc->getElementsByTagName("title")->item(0)->nodeValue; $videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue;

It was working before (This coding is working fine in Local server but on internet its not working) but now its not working. Please guide me how to fix this error. Thanks for your time.

Answer1:

Your server's allow_url_fopen is disabled (so is mine). I feel your pain. Here's what I did.

Try using <a href="http://php.net/manual/en/book.curl.php" rel="nofollow">cURL</a>, but return your data in json, using YouTube's v2 api. You do that by appending that data to the end of your url.

?v=2&alt=json

You didn't post how you're getting your YouTube ID, and that may be a part of the issue (though your sample url did work). So just in case, I'm also posting a simple function to retrieve the ID from the YouTube video url.

function get_youtube_id($url) { $newurl = parse_url($url); return substr($newurl['query'],2); }

OK, now assuming you have your video id, you can run the following function for each field you wish to return.

// Grab JSON and format it into PHP arrays from YouTube. // Options defined in the switch. No option returns entire array // Example of what the returned JSON will look like, pretty, here: // http://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=json&prettyprint=true function get_youtube_info ( $vid, $info ) { $youtube = "http://gdata.youtube.com/feeds/api/videos/$vid?v=2&alt=json"; $ch = curl_init($youtube); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); //If $assoc = true doesn't work, try: //$output = json_decode($output, true); $output = json_decode($output, $assoc = true); //Add the ['feed'] in if it exists. if ($output['feed']) { $path = &$output['feed']['entry']; } else { $path = &$output['entry']; } //set up a switch to return various data bits to return. switch($info) { case 'title': $output = $path['title']['$t']; break; case 'description': $output = $path['media$group']['media$description']['$t']; break; case 'author': $output = $path['author'][0]['name']; break; case 'author_uri': $output = $path['author'][0]['uri']; break; case 'thumbnail_small': $output = $path['media$group']['media$thumbnail'][0]['url']; break; case 'thumbnail_medium': $output = $path['media$group']['media$thumbnail'][2]['url']; break; case 'thumbnail_large': $output = $path['media$group']['media$thumbnail'][3]['url']; break; default: return $output; break; } return $output; } $url = "http://www.youtube.com/watch?v=oHg5SJYRHA0"; $id = get_youtube_id($url); echo "<h3><a href=" . $url . ">" . get_youtube_info($id, 'title') . "</a></h3>"; //echoes the title echo "

<a href=" . $url . "><img style='float:left;margin-right: 5px;' src=" . get_youtube_info($id, 'thumbnail_small') . " /></a>" . get_youtube_info($id, 'description') . "

"; //echoes the description echo "<br style='clear:both;' /><pre>"; echo print_r(get_youtube_info($id)); echo "</pre>";

Answer2:

DOMDocuments' load() function uses PHPs fopen wrappers to retrieve files. It seems that on your webserver, allow_url_fopen is set to 0, thus disabling these wrappers. Try adding the following line to the top of your script:

ini_set ('allow_url_fopen', 1);

UPDATE: Try this:

<?php $url = "http://gdata.youtube.com/feeds/api/videos/" . $embedCodeParts2[0]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $file = curl_exec($ch); curl_close($ch); $doc = new DOMDocument; @$doc->loadHTML($file); $title = $doc->getElementsByTagName("title")->item(0)->nodeValue; $videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue;

Answer3:

I hope it is not too late. My solution is to edit /etc/resolv.conf in your Linux machine: and replace first line with below line:

nameserver 8.8.8.8

Then save the file. no need for service restart.

Might work for servers who disabled some function accidentally for security.

Recommend

  • string manipulations
  • Cleaner way of handling addition of summarizing rows to table?
  • Import from different directories in python
  • StringBuilder vs ampersand equals concatentation
  • Generate table based on number of rows, columns in jquery
  • How to append to Android MediaRecorder output file
  • How to load XML “row” into index of listbox C#
  • Ios swift - storing uiimage in document directory and path in core data
  • check all images loaded after .ajax request?
  • Understand pipe and redirection command
  • youtube video insert “onBehalfOfContentOwner” parameter value
  • How to update data into a file in a particular position in js
  • Append the commit message automatically to the file being committed in Git
  • Why does _GET in PHP wrongly decodes slash?
  • How do I get the post request with express js?
  • Transpose table then set and rename index
  • Append Textarea result from another class
  • ConfigurationBuilder not working in azure function
  • Cannot page through all results using nextPageToken on YouTube search API v3
  • Unable to play media with vlc ocx
  • How to implement Deep Linking in Roku SG application?
  • android google indoor map
  • Play WS (2.2.1): post/put large request
  • Different response to non-authenticated users and AJAX calls
  • swift auto completion not working in Xcode6-Beta
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • How to stop GridView from loading again when I press back button?
  • need help with bizarre java.net.HttpURLConnection behavior
  • Turn off referential integrity in Derby? is it possible?
  • Is it possible to post an object from jquery to bottle.py?
  • Django query for large number of relationships
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize