PHP - Posting video on Facebook through Graph API using a valid access token -
PHP - Posting video on Facebook through Graph API using a valid access token -
i have video exist on server, need post video on facebook using graph api.
here code suggested team facebook.
what doing below.
1) android device getting access token
2) recognizing user passing access token facebook , email id , through email id recognize user
3) posting user's video server facebook through graph api.
4) returning video id android device api response.
i approaching route because in android device 2 step process post video on facebook.
1) download video first
2) post facebook
this time consuming.
here code trying
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 { // 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 seek 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 ); seek { $ret = $facebook->api('/me/videos', 'post', $params); print_r($ret); } catch(\exception $e) { echo " exception generated in seek 2 : ".$e->getmessage(); } die("</pre>"); } }
output getting an active access token must used query info current user.
error , (#353) must select video file upload.
look @ image
please tell me how solve problem ??
new code tried...................................................
/* code 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" ); seek { $ret = $facebook->api('/me/videos', 'post', $params); echo 'successfully posted facebook'; echo "<pre>";print_r($ret);echo "</pre>"; } catch(exception $e) { echo $e->getmessage(); }
but gives (#353) must select video file upload
error
here answer
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(); // code handle exception } homecoming $video_id; }
reference : how post video facebook through graph api using php
i don't know why info configurations
$config['fileupload'] = true; $config['cookie'] = true;
is not mentioned @ below official docs of apis
https://developers.facebook.com/blog/post/493/
https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos#publish
however solution given above, worked fine me.
php facebook facebook-graph-api video
Comments
Post a Comment