PHP - 使用有效的访问令牌通过Graph API在Facebook上发布视频

我的服务器上有一个视频,我需要使用Graph API在Facebook上发布该视频。

以下是Facebook团队建议的代码。

我在做什么如下。

1)从Android设备我获得访问令牌

2)通过将访问令牌传递给Facebook并获取电子邮件ID并通过电子邮件ID识别用户来识别用户

3)通过Graph API将用户的视频从我的服务器发布到Facebook。

4)将视频ID作为API响应返回到Android设备。

我正在接近这条路线,因为在Android设备中,在Facebook上发布视频是两步过程。

1)首先下载视频

2)发布到Facebook

这很费时间。

这是我正在尝试的代码

    define("FB_WEB_APP_ID","********");
    define("FB_WEB_SECRET","********");
    define("FB_WEB_REDIRECT_URI","<< redirect url >>");
    $GLOBALS["all_user_dir_path"]="/var/www/proj/web/video/user_videos/";
     define("FB_WEB_SCOPE","user_friends,email,public_profile,user_hometown,user_location,user_photos,user_videos,publish_actions,read_friendlists,publish_stream,offline_access");
    define("FB_WEB_RESPONSE_TYPE","code%20token");

    $GLOBALS["fb_app_creds"]=array();
    $GLOBALS["fb_app_creds"]['appId']= FB_WEB_APP_ID;
    $GLOBALS["fb_app_creds"]['secret']=FB_WEB_SECRET;
    $GLOBALS["fb_app_creds"]['response_type']=FB_WEB_RESPONSE_TYPE;
    $GLOBALS["fb_app_creds"]['redirect_uri']=FB_WEB_REDIRECT_URI;
    $GLOBALS["fb_app_creds"]['scope']=FB_WEB_SCOPE;
    $GLOBALS["facebook"] = new Facebook($GLOBALS["fb_app_creds"]);



class DefaultController extends Controller
{
// some code....

/**
     * @Route("/gk",name="_fb")
     * @Template()
     */
    public function gkAction(Request $request){

        $facebook = $GLOBALS["facebook"];

        $access_token=$request->query->get("access_token");

        if(!$access_token){
          die("give access token in url.......");
        }

        echo "<pre>";
        $facebook->setAccessToken($access_token);

        $user = $facebook->getUser();

        $me=$facebook->api("/me");

        $email=$me['email'];

        $all_user_dir_path=$GLOBALS["all_user_dir_path"];
        $user_directory = str_replace(array(".","@"), "_",$email);

        $user_dir_abs_path=$all_user_dir_path.$user_directory;

        print_r($me);

        $video_file_path=$user_dir_abs_path."/video.mp4";

        if(file_exists($video_file_path))
        { 
          echo "file exists...";
        }else{
          die("not exist");
        }

        $video_title="Test";
        $video_desc="Test";

        $access_token=$request->query->get("access_token");

        $file = "@".$video_file_path;
        $data =  array('name' => 'file', 'file' => $file);

          $post_url = "https://graph-video.facebook.com/me/videos?"
         . "title=" . $video_title. "&description=" . $video_desc
         . "&". $access_token
         ;


        echo "<hr>TRY 1<hr>";
        try{
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $post_url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            $res = curl_exec($ch);

            $video_id=0;
            if( $res === false ) {

            }else{
              $res=json_decode($res,true);
              /* $video_id = $res['id'];*/
               echo ":::: ";print_r($res);
            }
            curl_close($ch);
       }catch(\Exception $e){
           echo " Exception generated in Try 1 : ".$e->getMessage();
       }

       echo "<hr>TRY 2<hr>";

       $params = array(
            "access_token" => $access_token,
            "name"=>"file",
            "file" => "@".$video_file_path,
            "title" => $video_title,
            "description" => $video_desc
          );
          try {
              $ret = $facebook->api('/me/videos', 'POST', $params);
              print_r($ret);
          } catch(\Exception $e) {
            echo " Exception generated in Try 2 : ".$e->getMessage();
          } 

        die("</pre>");
    }    

}

我得到的输出是An active access token must be used to query information about the current user. 错误和(#353) You must select a video file to upload.

看看这张图片

在此处输入图像描述

请告诉我如何解决这个问题?

新代码尝试了............................................... ....

 /* code with sdk - object oriented way */
        $file=$GLOBALS["all_user_dir_path"].$user_directory."/video.mp4";
        $source = array();
        $source['name']="video.mp4";
        $source['type'] = "video/mp4";
        $source['tmp_name'] = $file;
        $source['error'] = 0;
        $source['size'] = filesize($file);
        echo "<br><br>$file<br><br>";

        $params = array(
          "access_token" => $access_token, 
            "source" => $source,
            "title" => "testvideo",
            "description" => "testvideo"
        );
          try {
              $ret = $facebook->api('/me/videos', 'POST', $params);
              echo 'Successfully posted to Facebook';
              echo "<pre>";print_r($ret);echo "</pre>";
          } catch(Exception $e) {
            echo $e->getMessage();
          } 

但是这给出了(#353) You must select a video file to upload错误

author: gkd
采纳答案:

这是答案

public function shareSocialgrationFB($access_token,& $exception){
        $video_id=0;

        try{
            $config = array();
            $config['appId'] = FB_WEB_APP_ID;
            $config['secret'] = FB_WEB_SECRET;
            $config['fileUpload'] = true; 
            $config['cookie'] = true;
            $facebook = new Facebook($config);
            $facebook->setFileUploadSupport(true);  
            $facebook->setAccessToken($access_token);

            $me=$facebook->api("/me");
            $email=$me['email'];

            $user_directory = str_replace(array(".","@"), "_",$email);

            $file = $GLOBALS["all_user_dir_path"].$user_directory."/video.mp4";

            $usersFacebookID=$facebook->getUser();
            $video_details = array(   
                      'access_token'=> $access_token,
                      'message'=> 'Testvideo!',
                      'source'=> '@' .realpath($file),
                      'title'=>'Test'   
            );
            $post_video = $facebook->api('/'.$usersFacebookID.'/videos', 'post', $video_details);
            $video_id=$post_video['id'];
        }catch(\Exception $e){
            //echo "Exception generated :: ".$e->getMessage(); 
        $exception=$e->getMessage(); 
        // extra code to handle exception
        }
        return $video_id;
    }

参考: 如何使用PHP通过Graph API将视频发布到Facebook

我不知道为什么有关配置的信息

$config['fileUpload'] = true; 
$config['cookie'] = true;

以下官方API文档中未提及

https://developers.facebook.com/blog/post/493/

https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos#publish

但是上面给出的解决方案对我来说很好。

author: gkd

参考更多解答: PHP - Posting video on Facebook through Graph API using a valid access token ,转载请保留出处PHP - 使用有效的访问令牌通过Graph API在Facebook上发布视频及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:video