VIMEO not consistently returning video title, duration or thumbnail url : REMOTE HEADER?
Asked Answered
U

1

1

I am using a wp_remote_get via Wordpress to retrieve information about a vimeo video. 80% of the time is works great, but there is this 20% factor, where the name, thumbnail_url and title are blank. The response from the wp_remote_retrieve_body is functioning, just the data is not there.

Here is my code:

function mmd_get_vimeo_info( $video_id ) 
 {
  $VimeoCommunicationLink  = sanitize_text_field(stripslashes(mmd_vimeoteaser_ReadSetting("`VIMEO_JSON_LINK")));  
  $VimeoConnectLink        = $VimeoCommunicationLink . $video_id;            
  $request  = wp_remote_get( esc_url_raw($VimeoConnectLink) );
  if ('error' == $request || is_wp_error($request))
    return ""; 
  
  $response = wp_remote_retrieve_body( $request );
  if ('error' == $response || is_wp_error($response))
    return ""; 
    
  $video_array = json_decode( $response, true );
  
  // Looking for [title], [thumbnail_url] [duration]
  return $video_array;
 }

typically, this returns the following:

$VimeoVideoData =  mmd_get_vimeo_info( $VideoId );
$VideoThumbNail =  $VimeoVideoData['thumbnail_url'];
$VideoTitle     =  $VimeoVideoData['title'];                      // Title of video
$Length         =  $VimeoVideoData['duration'];  

It appeared that this had something to do with domain privacy and that was confirmed when I got a 403 within domain_status_code. Here is the dump, when using print_r($response, true);

(
    [type] => video
    [version] => 1.0
    [provider_name] => Vimeo
    [provider_url] => https://vimeo.com/
    [html] => <iframe src="https://player.vimeo.com/video/304219740?h=914796992d&amp;app_id=122963" width="640" height="360" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>
    [width] => 640
    [height] => 360
    [domain_status_code] => 403
    [video_id] => 304219740
    [uri] => /videos/304219740
)

The mystery is, the video plays on the same site, it just does not always return "[title], [thumbnail_url] or [duration]. I found this page: https://developer.vimeo.com/api/oembed/videos Ït says: To get the complete response, including the private metadata, send the Referer header with the request, and set its value to the video's whitelisted domain.

But I have no idea how to do this with : wp_remote_get and wp_remote_retrieve_body. wp_remote_retrieve_header($request, $header);

Any ideas? `Could use some help. The successful playing of the video, is the mystery. I just need to consistently get the thumbnail, duration and title.

Uther answered 31/1, 2022 at 1:32 Comment(0)
U
3

Found it!

The solution to getting the Meta data from Vimeo, when the video is private is this, you have to use curl. I found no solution, but even standard curl does not do it. The key competent is to transfer the site domain through curl, so that the information can be validated.

$SiteUrl                 = get_site_url();
curl_setopt($ch, CURLOPT_REFERER, $SiteUrl)

The complete function call is below.

 function mmd_get_vimeo_info( $video_id ) 
 {
  $VimeoCommunicationLink  = 'https://vimeo.com/api/oembed.json?url=https://vimeo.com/'; 
  $VimeoConnectLink        = $VimeoCommunicationLink . $video_id ;
  $SiteUrl                 = get_site_url();  
  
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $VimeoConnectLink);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_REFERER, $SiteUrl); //<<<< THIS IS THE KEY

    $response = curl_exec($ch);
    if (curl_errno($ch)) 
      {
       curl_close($ch);
       echo 'Error:' . curl_error($ch);
       return "";                       // return a blank.
      }

    curl_close($ch);

   $video_array = json_decode( $response, true );
    
    // Looking for [title], [thumbnail_url] [duration]
  return $video_array;
 }
Uther answered 31/1, 2022 at 4:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.