1

I created a shortcode to retrieve videos from a channel ID, but it doesn't work without disabling SSL check. I've read that it's not a good idea to disable the check, so is there a better way make it work without disabling it?

Here is my code:

function latest_video_shortcode($atts = [], $content = null) {
$matches = [];
$output = '';
$res = '';
$atts  = shortcode_atts(
    array(
        'id'     => '',
        'items'  => '',
    ), $atts
);
if($atts['id'] == "") {
    $output = '';
} else {
    if($atts['items'] == "") {
        $number = '3';
    } else {
        $number = $atts['items'];
    }
    $id = $atts['id'];
    stream_context_set_default( [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]);
    $uploads = 'https://www.googleapis.com/youtube/v3/channels?part=contentDetails&key=KEY&id='. $id;
    $data1 = json_decode(file_get_contents($uploads), true);
    $uploads_object = $data1['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
    $url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$uploads_object.'&key=KEY&maxResults='.$number;
    $data2 = file_get_contents($url);
    $characters = json_decode($data2, true);
    for ($i = 0 ; $i < $number ; ++$i) {
        $link = $characters['items'][$i]['snippet']['resourceId']['videoId'];
        $img = $characters['items'][$i]['snippet']['thumbnails']['medium']['url'];
        $title = $characters['items'][$i]['snippet']['title'];
        $contenido = '<div class="video">
        <a target="_blank" href="https://www.youtube.com/watch?v='.$link.'">
          <div class="image">
               <img src="'.$img.'">
               <div class="vid"><i aria-hidden="true" class="fa fa-youtube-play"></i></div>
          </div>
        </a></div>';
        $res .= $contenido;
    }
    $output = '<div id="vid-feed">'.$res.'</div>';  
}
return html_entity_decode($output); 
}
add_shortcode('ytb_video', 'latest_video_shortcode');

And the shortcode works like this:

[ytb_video id="channelID" items="4"]
J.Doe
  • 67
  • 1
  • 8
  • If you turn off peer verification you make yourself vulnerable to MITM attacks. Take a look at the following post and discussion. Note the link to the curl bundle, and how to reference it: https://stackoverflow.com/a/28701786/9653083 – ansibly May 10 '18 at 20:44

0 Answers0