Posted in php functions
14772
2:04 am, November 4, 2021
 

youtube search api request function

This function requests a search from the youtube search api, and searches the text in the snippet for the seearch string. You can get your own API key for this from the google developers console. They allow you to have 10000 credits on the API (per day in the free tier), and one search equals 100 credits at the time of writing this, so you can get 100 searches in the free tier. 

I think its a good idea to restrict this API key to just access the youtube as well.

You can pass in the search_query into your function here, to get it to search for the string. 

Replace the $apikey with your own! :)

In this example it just adds all the values to $out and then returns out, but you can see what else is available in the search results by var_dump($data) if you want to see what is being returned or you can refer to the api docs. 

In my example you can see that i also add an iframe code, and also the video view url, which i didnt see in the feed as they only seem to provide the id, but maybe i just missed it. 

Also i added the max res thumbnail as $thumb_max as they seemed to miss this as well, even though some of the videos dont provide the max res one. 

PHP

public function youtube_search_api (
    $loop_max = 10
  ) {

      global $db;
      global $functions;
      $out = "";
      $for_counter = 0;
      $main_loop_tag = ".page_content_ctn";
      $db_table_name = $this->db->escapeString($this->db_table_name);
      $loop_max = $this->db->escapeString($loop_max);

      $search_query = $_GET['search_query'];
      $search_query_enc = urlencode($search_query);
      $search_query_decode = urldecode($search_query);

      /* $import_url_search = $this->import_url; */
      /* $import_url_search = str_replace ( "[@search_query]" , $search_query_enc , $import_url_search); // replace... [@search_term] */

      if($search_query >= "") {} else { return false; } // if no game url just return.

      /*
      save the html file for testing to see if the grabbed file is the same as the one in browser.
      echo $import_url_search;
      file_put_contents ( "./youtube_test.html" , file_get_contents($import_url_search) );

      search options

      snippet
      snippet.title

      */

      $apikey = "****** replace this with your api key *****";
      $max = 10;
      $youtube_api = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $search_query_enc . '&maxResults=' . $max . '&key=' . $apikey;

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $youtube_api);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_VERBOSE, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $response = curl_exec($ch);

      curl_close($ch);

      /* echo $json_string = json_decode($response, JSON_PRETTY_PRINT); */
      /* return; */

      $data = json_decode($response);
      /* var_dump($data); */
      $value = json_decode(json_encode($data), true);

      $result_number = $max;

      for ($i = 0; $i < $max; $i++) {
          $video_id = $value['items'][$i]['id']['videoId'];
          $title = $value['items'][$i]['snippet']['title'];
          $description = $value['items'][$i]['snippet']['description'];
          $thumb_default = $value['items'][$i]['snippet']['thumbnails']['default']['url'];
          $thumb_medium = $value['items'][$i]['snippet']['thumbnails']['medium']['url'];
          $thumb_high = $value['items'][$i]['snippet']['thumbnails']['high']['url'];
          $thumb_max = "https://img.youtube.com/vi/".$video_id."/maxresdefault.jpg";

          $out .= "\$video_id:$video_id<br />";
          $out .= "\$title:$title<br />";
          $out .= "\$description:$description<br />";
          $out .= "\$thumb_default:$thumb_default<br />";
          $out .= "\$thumb_medium:$thumb_medium<br />";
          $out .= "\$thumb_high:$thumb_high<br />";
          $out .= "\$thumb_max:$thumb_max<br />";
          $out .= "<img src='$thumb_max' />";
          $out .= "<hr />";

          $title_md5 = md5($title);

          $target_thread_class = new $this->target_thread_class;
          $target_thread_class->add_to_menu = false;
          $target_thread_class->start();

          if($target_thread_class->md5_exists($title_md5)) {
            // continue on to processing replies for this thread.
          } else {
            $target_thread_class->title = $title;
            $target_thread_class->md5 = $title_md5;
            $target_thread_class->additional = $description;
            $target_thread_class->category = $search_query_decode;
            $target_thread_class->search_term = $search_query_decode;
            $target_thread_class->video_id = $video_id;
            $target_thread_class->thumb_default = $thumb_default;
            $target_thread_class->thumb_medium = $thumb_medium;
            $target_thread_class->thumb_high = $thumb_high;
            $target_thread_class->thumb_max = $thumb_max;
            $target_thread_class->video_url = "https://www.youtube.com/watch?v=".$video_id;
            $target_thread_class->video_iframe = "<iframe width='560' height='315' src='https://www.youtube.com/embed/".$video_id."' title='YouTube video player' frameborder='0' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>";
            $target_thread_class->add();
            $target_thread_class->load_from_id($target_thread_class->id);
          }

        }

      echo $out;

  }

View Statistics
This Week
563
This Month
2402
This Year
4300

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
You drown not by falling in the river, but by staying submerged.
Unknown
Random CSS Property

max-block-size

The max-block-size CSS property specifies the maximum size of an element in the direction opposite that of the writing direction as specified by writing-mode. That is, if the writing direction is horizontal, then max-block-size is equivalent to max-height; if the writing direction is vertical, max-block-size is the same as max-width.
max-block-size css reference