Help with PHP code.

bankole1

Junior Member
Joined
Aug 2, 2012
Messages
165
Reaction score
42
Sorry for the noobie question, but how can I convert this PHP code into a working program?

Code:
<?php 
 
        /**
         * Tubelogix
         */
        function fetch_youtube_links($url)
        {
                $__types = array( 'video/x-flv' => 'FLV', 'video/webm' => 'WEBM',  'video/mp4' => 'MP4', 'video/3gpp' => '3GP');
       
                $url_components = parse_url($url);
                if($url_components === false || !isset($url_components['query']))
                        return false;
               
                $query_vars = array();
               
                parse_str($url_components['query'], $query_vars);
                if(!isset($query_vars['v']))
                        return false;
               
                $id = $query_vars['v'];
               
                $infos_url = 'http://www.youtube.com/get_video_info?video_id='.$id.'&asv=3&el=detailpage&hl=en_US';
               
                $ch = curl_init($infos_url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $infos_content = curl_exec($ch);
                curl_close($ch);
               
                $infos = array();
               
                parse_str($infos_content, $infos);
               
                if(isset($infos['errorcode']) || !isset($infos['url_encoded_fmt_stream_map']))
                        return false;
               
                $links = explode(',', $infos['url_encoded_fmt_stream_map']);
               
                if(($number_of_vids = count($links)) === 0)
                        return false;
               
                $result = array('infos' => array(), 'links' => array());
               
                $result['infos']['video_id']    = isset($infos['video_id'])             ? $infos['video_id']                            : '';
                $result['infos']['title']               = isset($infos['title'])                        ? $infos['title']                                       : '';
                $result['infos']['length']              = isset($infos['length_seconds'])       ? $infos['length_seconds']                      : '';
                $result['infos']['author']              = isset($infos['author'])                       ? $infos['author']                                      : '';
                $result['infos']['thumbnail']   = isset($infos['thumbnail_url'])        ? $infos['thumbnail_url']                       : '';
                $result['infos']['img']                 = isset($infos['iurlsd'])                       ? $infos['iurlsd']                                      : '';
                $result['infos']['keywords']    = isset($infos['keywords'])             ? explode(',',$infos['keywords'])       : '';
                $result['infos']['views']               = isset($infos['view_count'])           ? $infos['view_count']                          : '';
               
                foreach($links as $link)
                {
                        $infos_link = array();
                        parse_str($link, $infos_link);
                       
                        if(isset($infos_link['url']) && isset($infos_link['sig']))
                        {
                                $url = $infos_link['url'].'&signature='.$infos_link['sig'].'&title='.$result['infos']['title'];
                                $type = '';
                                $quality = isset($infos_link['quality']) ? $infos_link['quality'] : '';
                                if(isset($infos_link['type']))
                                {
                                        $data_type = explode(';', $infos_link['type']);
                                        $mime_type = trim($data_type[0]);
                                        if(array_key_exists($mime_type, $__types))
                                                $type = $__types[$mime_type];
                                }
                               
                                //Optional : fetch the size of the video
                                $head = get_headers($infos_link['url'].'&signature='.$infos_link['sig']);
                                $size = '';
                                foreach($head as $header)
                                        if(strpos(trim($header), 'Content-Length:') === 0)
                                        {
                                                $size = trim(substr($header, 15));
                                                break;
                                        }
                                       
                                $result['links'][] = array('url' => $url, 'type' => $type, 'quality' => $quality, 'size' => $size);
                        }
                       
                }
               
                return $result;
        }
 
        $url = 'http://www.youtube.com/watch?v=Rgox84KE7iY';
        var_dump(fetch_youtube_links($url));
 
I haven't check trough the code but your host need to support php, save the file just like you do with html but with a .php extension instead.
Also, you have an opening tag (<?php) but no closing tag (?>).

If you have no host you can run it locally with Xampp.
 
I haven't check trough the code but your host need to support php, save the file just like you do with html but with a .php extension instead.
Also, you have an opening tag (<?php) but no closing tag (?>).

If you have no host you can run it locally with Xampp.

Thanks alot. can you go through the code and help me fix the problems?
 
Thanks alot. can you go through the code and help me fix the problems?

Fix what problem? About the closing tag, just add "?>" at the end.

Just like HTML uses <html>...</html>, php uses <?php ... ?>
 
That's a download link generator for youtube videos. You could host it to a server, but the download links would only work with the server's IP. So you could try xampp as Schvamp said, to be able to access the download links with your local IP.
 
Back
Top