Posting an embedded video link using the Facebook Graph API

后端 未结 7 1967
独厮守ぢ
独厮守ぢ 2020-12-02 06:29

When manually attaching a video link (from YouTube, Vimeo, etc) to a post using the Facebook web interface, Facebook automatically recognizes the link as a video, and allows

7条回答
  •  长情又很酷
    2020-12-02 07:14

    Here's how to post a video manually for YOUTUBE and VIMEO (hard to find online). Specifically if you want to have the LINK value pointing to a user's website/blog post where it originates.

                    //search for youtube.com and vimeo.com in the 'link' value
                    if (preg_match("/youtube.com/", $model->link) || preg_match("/youtu.be/", $model->link)){
                        if (preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $model->link, $match))
                        {
                            $video_code = $match[1];
                        }
                       $source = 'http://www.youtube.com/e/'.$video_code; 
                   $picture = 'http://img.youtube.com/vi/'.$video_code.'/0.jpg';
                    }
                    else if (preg_match("/vimeo.com/", $model->link))
                    {
                        if (preg_match('/vimeo\.com\/(clip\:)?(\d+).*$/', $model->link, $match))
                        {
                            $video_code = $match[2];
                         }
                        /* Get Vimeo thumbnail */
                        $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video_code.php"));
                        $picture = $hash[0]['thumbnail_medium'];  
                        $source = 'https://secure.vimeo.com/moogaloop.swf?clip_id='.$video_code.'&autoplay=1';
                    }
    
                    $args = array(
                    'message'   => //user's comment
                    'name' => //Title of post
                    'link'      => 'http://...'//link to video on user's website
    
                    'source' => $source,
                    'picture' => $picture,
                    );
    
                    if ($this->_facebook->api("/".$this->facebookUserID."/feed", "post", $args)){
                    //posted to facebook
                    }
    

提交回复
热议问题