Posted in php functions
14617
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
511
This Month
2247
This Year
4145

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

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

user-select

The user-select CSS property controls whether the user can select text. This doesn't have any effect on content loaded as part of a browser's user interface (its chrome), except in textboxes.
user-select css reference