SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Critical Feedback Wanted - New to This.

    I'm pretty new to programming and PHP. This is the first language that I am attempting to learn (aside from HTML and CSS).

    Part of this script was taken off the net, part of it I wrote. It is used to pull some information from a youtube feed and then input that data dynamically into a jQuery rotator.

    I'm just looking for some constructive criticism if anyone is bored. Just want to get better at this. :-)

    Code:
    <?php 
    
    function some_video_rotator() {
    
    	//these are the settings for your youtube feed and display
    		$settings = array(
    			'user' => 'ayoutubeuser', //YouTube user
    			'limit' => 5, //number of videos to pull
    			'width' => 300, //width of video in pixels
    			'height' => 200 //height of video in pixels	
    		);
    		
    		
    		//this is the youtube api feed data location, stored as an array in '$data'
    		$data = @json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/users/'.$settings['user'].'/uploads?alt=json'), TRUE);
    		
    		
    		// some definitions...
    		$counter = 0;
    		$a = 0;
    		$url_store = array();
    		$title_store = array();
    		$ycontent_store = array();
    		
    		
    		foreach($data['feed']['entry'] as $vid)
    			{
    				// as $a increments it asigns each video url to the $url_store array...
    				// this functions for the title and video as well
    				$a++;
    				$url = $vid['media$group']['media$content'][0]['url'];
    				$url_store[$a] = $url;
    				
    				//assigns to $title_store array
    				$title = $vid['title']['$t'];
    				$title_store[$a] = $title;
    
    				// assigns to $ycontent_store array
    				$ycontent = $vid['content']['$t'];
    				$ycontent_store[$a] = $ycontent;
    				
    			
    			// counter was set at '0'. This says if counter is equal to the 'limit' (in settings
    			// to break the increase counting.
    			$counter++;
    				if($counter == $settings['limit'])
    				{
    					break;
    				}
    	
    		} 
    
    ?>
    the $url_store, $title_store, and $ycontent stores are then used within HTML to pull the parts of the youtube feed I need. Most of the comments are for me (cause I won't remember that stuff later).

    Thoughts anyone? Thanks in advance.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    When making request to external resources it is best to use cURL rather than file_get_contents(). This is because file_get_contents() has no timeout whereas, timeout can be controlled and handled properly with cURL – file_get_contents() will run until the php execution time is hit. Also, using @ to suppress errors is a very bad habit. Get in the habit of writing proper error and exception handling.
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oddz

    Thank you very much for responding. I've not yet come across cURL so I will look that up in the manual.

    Admittedly, I did not write that particular portion of this script. How might I properly handle errors in this context? Or, could you point me someplace I can read on that specifically?

    I didn't even know what the @ was for until now. So, thank you for sharing your expertise.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •