Home > Back-end >  php checking remote image via curl doesnt work every now and then
php checking remote image via curl doesnt work every now and then

Time:08-03

Hello so i am web developer and i have this script where client puts youtube link and i just yank the video id and get youtube thumbanil output it to the site, the only thing that some videos dont have maxresdefault thumbnial so i check if maxresdefault thumbnail exist and if so use it, if not use hqdefault the problem is like right now, no one was editing code or editing anything on the server, but the curl command started to hang, it happens every now and then and i have to remove the curl code for a while until it starts working again, i dont know why it keeps happening the questin is, why is this happening, can i somehow check if the curl command is gonna hang if so just skip the check and use hqdefault thumbnail, i am putting my

// i also have support for vide, the checking if video is vimeo or youtube is ofscren also the yanking of youtbe id
// youtube idk for example jd8G-Qe2PAc, is in the $imgurl var , if the video was vimeno there is id to vimeo   
if ($typ_videa == 'vimeo') {
                echo "<img class='' loading='lazy' src='$imgurl' alt='video'></a>";
            } else {
                
//here start the checking process
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$imgurl);
                // don't download content
                curl_setopt($ch, CURLOPT_NOBODY, 1);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');  
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                // docasne settingy pre debuggovanie
                curl_setopt($ch, CURLOPT_VERBOSE, TRUE);


//here find if check was succes
                $result = curl_exec($ch);
                curl_close($ch);
/if exists use maxresdefault
                if($result !== FALSE)
                {
                    echo "<img class='' loading='lazy' src='$imgurl' alt='video'>";

                }
/if not use hqdefault, also hqdefault have worng aspect ratio, thats why the use as image background and cropping it to 16:9
                else
                {
                    echo "<div style='height:0px;padding-bottom:56.23%;background:url(https://i1.ytimg.com/vi/{$imgPath}/hqdefault.jpg) no-repeat center; 
                    background-size:cover;' loading='lazy' alt='video' ></div>";
                }
            }
//adding class depending if the video was vimeo or youtube
            echo '<span vimeo";
            } else {
                echo "youtube";
            }
            echo '"></span></a>'; 

so does anybody can tell my why is my code hanging once in a while? is the issue with youtube? i chceck and the thumbnail load correctly in my browser and pings go through, the issue is with curl i think, it stops working idk why. i one thought i would fix it with downloading locally the thumbnails, but if the thumbnails dont work what is the cahcne video will work. i just probably need to chcek first if curl is working, can i check it somehow or is there any workaround ?

CodePudding user response:

well i solved it by download each thumbnail and than loading it my solution is as follows

if (file_exists("thumbnails/" .$imgPath)) {
                echo "<img class='' loading='lazy' src='{$config->urls->templates}thumbnails/$imgPath' alt='video'>";
            } else {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$imgurl);
                // don't download content
                curl_setopt($ch, CURLOPT_NOBODY, 1);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');  
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                // docasne settingy pre debuggovanie
                curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
                
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); 
                curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
                $result = curl_exec($ch);
                #$result = FALSE;
                curl_close($ch);
                if($result !== FALSE) {
                        // Initialize the cURL session
                        $ch = curl_init($imgurl);
                    
                        // Initialize directory name where
                        // file will be save
                        $dir =  "thumbnails/";
                        if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
                                mkdir( $dir );
                        } 
                        // Use basename() function to return
                        // the base name of file
                        $file_name = basename($imgPath);
                        
                        // Save file into file location
                        $save_file_loc = $dir . $file_name;
                        // Open file
                        $fp = fopen($save_file_loc, 'wb');
                        
                        // It set an option for a cURL transfer
                        curl_setopt($ch, CURLOPT_FILE, $fp);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        
                        // Perform a cURL session
                        curl_exec($ch);
                        
                        // Closes a cURL session and frees all resources
                        curl_close($ch);
                        
                        // Close file
                        fclose($fp);

                } else {
                        // Initialize the cURL session
                        $ch = curl_init("https://i1.ytimg.com/vi/{$imgPath}/hqdefault.jpg");

                        // Initialize directory name where
                        // file will be save
                        $dir =  "thumbnails/";
                        if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
                                mkdir( $dir );
                        } 
                        // Use basename() function to return
                        // the base name of file
                        $file_name = basename($imgPath);
                        
                        // Sa               /* if ($user->isLoggedin()) { */= fopen($save_file_loc, 'wb');
                        
                        // It set an option for a cURL transfer
                        curl_setopt($ch, CURLOPT_FILE, $fp);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        
                        // Perform a cURL session
                        curl_exec($ch);
                        
                        // Closes a cURL session and frees all resources
                        curl_close($ch);
                        
                        // Close file
                        fclose($fp);
                }
                echo "<img class='' loading='lazy' src='{$config->urls->templates}thumbnails/$imgPath' alt='video'>";
            }

the answer why i am checking first downloading once and then second well firstly i check if maxresdefault exists if so download it and if not download hqdefault downloads works flawlessy, there is some processwire's API so it wont work on your device without some modifications

  • Related