We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8653
    • 4 Posts
    I’m going slightly crazy here, this here code below works real nice when I run it straight as php code but as soon as I save it as a snippet it doesnt work.

    <?php
    
    $ypp_feed_url = "http://gdata.youtube.com/feeds/playlists/4A4F1225777AD974";
    
    $ypp_video_width=300;
    $ypp_video_height=251;
    
    $ypp_headline_key = "*FEED*ENTRY*TITLE"; //custom title
    $ypp_description_key = "*FEED*ENTRY*YT:DESCRIPTION"; //custom description
    $ypp_original_title = "*FEED*ENTRY*MEDIA:GROUP*MEDIA:TITLE";//original title ATTRIBUTE
    $ypp_original_description_key = "*FEED*ENTRY*MEDIA:GROUP*MEDIA:DESCRIPTION"; //original description ATTRIBUTE
    $ypp_player_url_key = "*FEED*ENTRY*MEDIA:GROUP*MEDIA:PLAYER"; //player url ATTRIBUTE
    $ypp_player_entry_key = "*FEED*ENTRY"; //EACH VIDEO IS IN ONE ENTRY WRAPPER
    
    $ypp_video_array = array();
    $ypp_counter = 0;
    $output = ''; //final output
    
    
    function ypp_showVideo($url){
    	global $ypp_video_width, $ypp_video_height;
    
    	$t=explode("=", $url);
    	$file = $t[1];
    
    	$out = '<object width="'.$ypp_video_width.'" height="'.$ypp_video_height.'">
    	  <param name="movie" value="http://www.youtube.com/v/'.$file.'" />
    	  <param name="wmode" value="transparent" />
    	  <embed src="http://www.youtube.com/v/'.$file.'&color1=0x5d1719&color2=0xcd311b&border=1" width="'.$ypp_video_width.'" height="'.$ypp_video_height.'" type="application/x-shockwave-flash" wmode="transparent" />
    	</object>';
    
    		return $out;
    }
    
    function ypp_startTag($ypp_parser, $ypp_data, $ypp_attributes){
        global $ypp_current_tag, $ypp_counter, $ypp_video_array;
    	global $ypp_player_entry_key, $ypp_player_url_key;
    
        $ypp_current_tag .= "*$ypp_data";
    
    	if($ypp_current_tag == $ypp_player_url_key){
    			$ypp_video_array[$ypp_counter]['player_url'] =  $ypp_attributes['URL'];
    	}
    }
    function ypp_endTag($ypp_parser, $ypp_data){
        global $ypp_current_tag, $ypp_counter, $ypp_player_entry_key;
    
        if($ypp_current_tag == $ypp_player_entry_key){
    		$ypp_counter++;
    	}
        $tag_key = strrpos($ypp_current_tag, '*');
        $ypp_current_tag = substr($ypp_current_tag, 0, $tag_key);
    }
    function ypp_contents($ypp_parser, $ypp_data){
        global $ypp_current_tag;
    	global $ypp_player_url_key, $ypp_headline_key;
    	global $ypp_description_key, $ypp_counter;
    	global $ypp_video_array, $ypp_original_title_key;
    	global $ypp_original_description_key, $ypp_player_entry_key;
    
        switch($ypp_current_tag){
        	case $ypp_player_entry_key:
        		break;
                $ypp_video_array[$ypp_counter]['headline'] = $ypp_data;
                break;
            case $ypp_original_title:
                $ypp_video_array[$ypp_counter]['original_title'] = $ypp_data;
                break;
            case $ypp_original_description_key:
                $ypp_video_array[$ypp_counter]['original_description'] = $ypp_data;
                break;
            case $ypp_description_key:
                $ypp_video_array[$ypp_counter]['description'] = $ypp_data;
                break;
        }
    }
    
    	$ypp_parser = xml_parser_create();
    	xml_set_element_handler($ypp_parser, 'ypp_startTag', 'ypp_endTag');
    	xml_set_character_data_handler($ypp_parser, 'ypp_contents');
    	if( !($ypp_datastream = fopen($ypp_feed_url, 'r'))){
    		$output .='<p>Could not open file</p>';
    	}
    	if ( !($ypp_data = stream_get_contents($ypp_datastream) )){
    		$output .='<p>Could not read file</p>';
    	}
    	if( !(xml_parse($ypp_parser, $ypp_data, feof($ypp_datastream) ) ) ){
    		$output .= '<p>Error on line ' . xml_get_current_line_number($ypp_parser) .'</p>' ;
    	}
    	fclose($ypp_datastream);
    
    $output .= "\n";
    
    for($x=0; $x < count($ypp_video_array); $x++){
    
    	if($ypp_video_array[$x]['description'] != ""){
    		$ypp_t_desc = $ypp_video_array[$x]['description'];
    	}else{
    		$ypp_t_desc = $ypp_video_array[$x]['original_description'];
    	}
    	if($ypp_video_array[$x]['headline'] != ""){
    		$ypp_t_title = $ypp_video_array[$x]['headline'];
    	}else{
        	$ypp_t_title = $ypp_video_array[$x]['original_title'];
    	}
    	$ypp_t_video = ypp_showVideo($ypp_video_array[$x]['player_url']);
    
    
    	$output_chunkArr = array('video' => $ypp_t_video,'title' => $ypp_t_title,'description' => $ypp_t_desc);
    
            $output .= $modx->parseChunk('YouTubePlaylist', $output_chunkArr, '[+', '+]');
    
    }//end for
    
          return $output;
    ?>
    


    The only changes period from the raw script which works and the snippet is this:
    	$output_chunkArr = array('video' => $ypp_t_video,'title' => $ypp_t_title,'description' => $ypp_t_desc);
            $output .= $modx->parseChunk('YouTubePlaylist', $output_chunkArr, '[+', '+]');
    


    and even when I take those lines out and print the output directly or return the output preformatted without the chunk it still doesnt work as a snippet.

    any help would be greatly appreciated!
      • 4310
      • 2,310 Posts
      From what I’ve seen elsewhere on the Forums you may have to declare
      global $modx;
      instead of
      global
      for your functions to work
        • 8653
        • 4 Posts
        hmm... i’ll try that thanks!
          • 8653
          • 4 Posts
          nope no luck... :’( but at least now I’m 100% sure that even as sloppy as my code is, it works across various systems and hosts... just not in modx...
            • 7231
            • 4,205 Posts
            Time for some debugging. Add some echo statements at the end of the key functions to track the process. MODx is a bit picky with some functions, they work outside but need to be adjusted to work inside of modx. I have not enough experience to see a trend but it happens a bit. Just need to find the culprit.

              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 8653
              • 4 Posts
              ok thanks guys I finally got it to work... i just converted most of the code to a class and the sent the array to be parsed by modx and voila! it worked... i guess i should just have done that from the start but i wanted to make sure the code ran in modx first.