Dante Cullari
Dante Cullari

Reputation: 769

Include Call to Action in Facebook Video Upload to Page - Graph API

I have the following working code for video upload to a Facebook page Timeline:

$file = $my_video;
$video_title = $my_title;
$video_desc = $my_description;
$post_url = "https://graph-video.facebook.com/".$fb_page_id."/videos?"
         . "title=" . $video_title. "&description=" . $video_desc
         . "&access_token=". $access_token;

$ch = curl_init();
$data = array('name' => 'file', 'file' => '@'.realpath($file));// use realpath
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);  
curl_close($ch);
$video_id = $res['id'];

I would like to add a call to action to the request, which is documented here from Facebook.

I tried the following:

$fb_action = strtoupper($the_action);
$fb_action_link = $action_link;
$call_to_action = "&call_to_action={'type':'".$fb_action."','value':{'link':'".$fb_action_link."'}}";

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

And I also tried:

$call_to_action = "&call_to_action=['type','".$fb_action."','value',['link','".$fb_action_link."']]";

I keep getting an error back immediately from Facebook. Does anyone know if this is a formatting issue? Any suggestions for the proper way? Hugely appreciated !

Upvotes: 0

Views: 984

Answers (1)

Dante Cullari
Dante Cullari

Reputation: 769

So the issue was really the formatting of the arrays. I found out that http_build_query() was the solution which is apparently how Facebook developers encode their params. This post helped me a lot - > passing arrays as url parameter

The final solution is here:

$action_call_array = array('type' => $fb_action, 'value' => array('link' => $fb_action_link));
$params = http_build_query(array('call_to_action' => $action_call_array));

$post_url = 'https://graph-video.facebook.com/'.$fb_page_id.'/videos?'
         . 'title=' . $video_title. '&description=' . $video_desc . '&' . urldecode($params) 
         . '&access_token='. $m_page_access_token;

Plug $post_url into curl then and works great! Hope this helps someone, took me a while to find and test the right answer, and looks like this solution would work for other Facebook endpoints with arrays in the params.

Upvotes: 1

Related Questions