Uploading video to old Facebook REST API - php

I'm having lots of issues with uploading videos.
If I try to use https://api-video.facebook.com I am getting a cURL host not found error, if I use http://api-video.facebook.com I get a message to use https://api-video.facebook.com
If I try to use https://api.facebook.com/restserver.php?method=video.upload I get a 101 error code -
<error_msg>Invalid API key</error_msg>
but the API key works for everything else, statuses, comments, likes, fql for the user?
Heres what I am sending:
access_token=XXXX
api_key=XXXX
call_id=1279204007.6003
description=Description+of+this%3F
format=JSON
title=Title%2C+a+title
v=2.0
sig=XXX
I read in the post on the FB developers forum that splitting the session key by | gives you a correct session key? Is this the same as access_token? I have tried splitting this up with no luck.
Any ideas, or even working code in PHP (!) would be most welcome! Thanks

Try using this code with the FB SDK
require_once 'facebook.php';
$appapikey = 'xxx';
$appsecret = 'xxx';
$facebook = new Facebook($appapikey, $appsecret);
$session_key = 'xxx'; //this is the infinite session_key returned when asking for the offline_access extended permission
$args = array(
'method' => 'facebook.video.upload',
'v' => '1.0',
'api_key' => $appapikey,
'call_id' => microtime(true),
'format' => 'JSON',
'session_key' => $session_key,
'title' => 'My video title',
'description' => 'My video description'
);
ksort($args);
$sig = '';
foreach($args as $k => $v) {
$sig .= $k . '=' . $v;
}
$sig .= $appsecret;
$args['sig'] = md5($sig);
$args["short.wmv"] = '#E:\path\to\short.wmv';
$ch = curl_init();
$url = 'http://api-video.facebook.com/restserver.php?method=facebook.video.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
print_r($data); //returned xml here
I also found a bug report submitted today stating that video uploads have been working and not working sporatically. It could be your code is just fine and facebook's APIs are messing up.
EDIT:
Try the following, it seems to have worked for a few people.

Related

curl not working but not giving any error either

I'm working on Globe Labs API to send an SMS. But before I can send an SMS, the mobile number I want to send message to needs to be subscribed so I can get the access_token which will be used in sending SMS.
There are 2 options to subscribe - via SMS or via Web Form. I was able to use first option without any problem. But I can't make the second option work.
According to the documentation, after the subscriber keyed-in the received confirmation pin on the page and clicked the Confirm button to authorize the subscriber, the page will then be redirected to the redirect_uri of my application, and a Code parameter will be passed(via GET) to it.
Here's the part where I fail to make it work:
To get the access token, you need to do a POST request via https://developer.globelabs.com.ph/oauth/access_token with your ‘app_id’, ‘app_secret’ and ‘code’ as the parameters. The parameters ‘access_token’ and ‘subscriber_number’ will then be returned to your Redirect URI as a response.
Here's my code:
$app_id = '<my_app_id>';
$app_secret = '<my_app_secret>';
$content = array(
'app_id' => $app_id,
'app_secret' => $app_secret,
'code' => $this->input->get('code')
);
$url = 'http://developer.globelabs.com.ph/oauth/access_token';
$this->post_to_url($url, $content);
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
if(!$result){
die('Error: "' . curl_error($post) . '" - Code: ' . curl_errno($post));
} else {
$this->Sms_Api_model->save_subscriber_data();
}
}
This is my redirect URL: http://api.forexcargo.us/sms_api/globelabs_api?code=[code]
And the result I get:
Error: "" - Code:
EDIT:
I tried to use a form and send my data via method POST and it worked. So it really might be my curl setup.
What am I doing wrong?
Any help is highly appreciated. Thank you!
Apologies for being a newbie regarding curl. Apparently, I had to add the following code to see the errors on my code because the URL I'm using has its error checking disabled:
error_reporting(-1);
ini_set('display_errors', 1);
set_time_limit(0);
And my new code:
function do_post_request($post_data)
{
error_reporting(-1);
ini_set('display_errors', 1);
set_time_limit(0);
$url = 'https://developer.globelabs.com.ph/oauth/access_token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
if($this->json_validator($response) == 1){
$decode_data = json_decode($response, true);
$post_data_arr = array('access_token' => $decode_data['access_token'], 'subscriber_number' => $decode_data['subscriber_number']);
$this->Sms_Api_model->save_subscriber_data($post_data_arr);
//var_dump(http_response_code(200));
}
}
$content = [
'app_id' => $app_id,
'app_secret' => $app_secret,
'code' => $this->input->get('code')
];
$post_request = $this->do_post_request($content);
Now it's working and I was able to save the data I received in my database. Thank you everyone for your help!

Facebook PHP SDK & Page feed post (filtering results)

Facebook PHP SDK & Page feed post (filtering results)
Greetings! I've been working lately on an effective way to implement a Facebook public page feed (each post featuring description, date and picture) on a website.
I've put put together the following code wich allows me to use a foreach on $elements.
$pageid = '#PAGEID#';
$accesstoken = '#ACCESSTOKEN#';
$url = "https://graph.facebook.com/v2.7/$pageid/feed?limit=20&access_token=$accesstoken";
function getfb($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_REFERER, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$raw_xml = curl_exec($curl); // execute the curl command
$result = json_decode($raw_xml, true);
return $result;
}
$elements = getfb($url);
foreach($elements['data'] as $k => $v){
$url = "https://graph.facebook.com/v2.7/{$v['id']}?fields=full_picture,picture&access_token=$accesstoken";
$fields = getfb($url);
$elements['data'][$k]['pictures'] = $fields;
}
var_dump($elements);
It does work nicely, but unfortunately instead of listing only the posts published on the page by the owner, it also lists posts published inside the box "Visitor Posts"... which I do not want.
Do you know and/or can help me figure out how to filter those results in such a way to only list posts published by page owner?
Thank you very much!
Thanks to the suggestions Luschn and CBroe posted I kept on reading/looking and endend up using the following code. I'm not sure yet if this is the correct/best way of doing so, but it does seem to working nicely.
require_once ('facebook/autoload.php'); // See https://developers.facebook.com/docs/reference/php/
$facebook_page_id = 'xxx';
$facebook_app_secret = 'yyy';
$facebook_app_id = 'zzz';
$facebook_graph_version = 'v2.6';
$fb = new Facebook\Facebook([
'app_id' => $facebook_app_id,
'app_secret' => $facebook_app_secret,
'default_graph_version' => $facebook_graph_version
]);
$response = $fb->get( '/'.$facebook_page_id.'/posts?fields=message,full_picture,link,updated_time,picture&limit=5', $fb->getApp()->getAccessToken() );
$get_data = $response->getDecodedBody(); // for Array resonse
foreach ( $get_data['data'] as $single ) {
var_dump($single);
}

Access Token still valid for 2 months but API response says #2500

I am trying to upload a photo to user's album with current script:
$this->facebook->api('/me/photos?access_token='.$user->access_token, 'post', $args);
$user->access_token is the user's access token and I check via Facebook Graph API Explorer tool it says still valid (until 2 months).
However if I create API request with script above, my console always returned:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
What I've been doing is:
deauthorize app
re-authorize app
request permission (publish_stream,photo_upload,user_photos)
update FB SDK to latest version
Here is the full code:
$user = $this->User_model->get_access_token($this->input->post('fb_id'));
foreach($this->input->post('media') as $media_id)
{
$media = $this->Media_model->get_media($media_id);
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath(FCPATH.'uploads/booth_1/'.$media->file);
$data = $this->facebook->api('/me/photos?access_token='.$user->access_token, 'post', $args);
print_r($data);
}
Any help?
FYI I am using codeigniter. And actually I was trying hours ago (it worked), however, now is not, because of access_token.
Thanks.
I solved it manually by NOT using built-in $facebook->api() but I am using curl instead.
$filename = '#' . realpath(FCPATH.'uploads/'.$media);
$url = "https://graph.facebook.com/".$user->fb_id."/photos";
$ch = curl_init($url);
$attachment = array('access_token' => $user->access_token,
'app_id' => APP_ID,
'name' => "Upload",
'fileUpload' => true,
'message' => "Message",
'image' => $filename,
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CAINFO, PATH TO FB SDK CERT FOR SSL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
if ($result === FALSE) {
die("Curl failed: " . curl_error($ch));
}
curl_close ($ch);

Facebook GRAPH API + PHP SDK: get the URL of the large user picture

I'm trying to rewrite a Facebook app (a PHP script embedding a small multiplayer Flash game) from old FBML to new iFrame type and it kind of works:
<?php
require_once('facebook.php');
define('FB_API_ID', '182820975103876');
define('FB_AUTH_SECRET', 'XXX');
$facebook = new Facebook(array(
'appId' => FB_API_ID,
'secret' => FB_AUTH_SECRET,
'cookie' => true,
));
if (! $facebook->getSession()) {
printf('<script type="text/javascript">top.location.href="%s";</script>',
$facebook->getLoginUrl(
array('canvas' => 1,
'fbconnect' => 0,
#'req_perms' => 'user_location',
)));
} else {
try {
$me = $facebook->api('/me');
$first_name = $me['first_name'];
$city = $me['location']['name'];
$female = ($me['gender'] != 'male');
$fields = $facebook->api('/me', array(
'fields' => 'picture',
'type' => 'large'
));
$avatar = $fields['picture'];
# then I print swf tag and pass first_name;city;avatar to it
} catch (FacebookApiException $e) {
print('Error: ' . $e);
}
}
?>
but I think that the call to get the user profile picture causes my script to perform a 2nd CURL fetch, which is probably avoidable? And also I'd like to use the new GRAPH API and not the old REST API - but I'm not sure how to rewrite that call (and I need to get the large and direct user picture).
If you know the user ID, just use:
<img src="http://graph.facebook.com/<UID>/picture?type=large" />
Also note that you can use that URL to retrieve the contents via cURL. If necessary, you can also use cURL to follow the redirects and get the final URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"http://graph.facebook.com/<UID>/picture?type=large");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
var_dump($url);
Instead of making two API calls you could use FQL, something like:
$result = $facebook->api(array(
'method'=>'fql.query',
'query'=>'SELECT uid,name,first_name,current_location,sex,pic_big FROM user WHERE uid=me()'
));
Sure the fql.query is not a graph method but still it's the way to use FQL.

Exception when uploading photo with Facebook Graph API

I would like to upload a photo to facebook for a user in the default album for an application. This is described under publishing here:
http://developers.facebook.com/docs/reference/api/photo
The method has been answered here: How can I upload photos to album using Facebook Graph API. I am using the following:
$args = array(
'message' => 'Photo Caption',
'image' => '#'.realpath("image.png")
);
$data = $facebook->api('/me/photos', 'post', $args);
However I get the exception "(#324) Requires upload file" when I attempt this. I have a valid session and I have the publish_stream and user_photos permissions. I can retrieve data using the API. The image file is definitely valid because it can be loaded with file_get_contents(realpath("image.png")).
I've tried this solution, using curl, which works perfectly:
Upload Photo To Album with Facebook's Graph API
$args = array(
'message' => 'Photo from application',
'pic.png' => '#'.realpath('pic.png')
);
$tok = $session['access_token']
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$tok;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Compared with Facebook's PHP SDK curl which looks like this (using the same $args and $url):
$ch = curl_init();
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = http_build_query($args, null, '&');
$opts[CURLOPT_URL] = $url;
curl_setopt_array($ch, $opts);
$data= curl_exec($ch);
Why doesn't the PHP version work? Looks like the http_build_query() function is interfering with loading the image. I don't know enough about curl to understand what's going on here.
I'm so glad I went trough the same problem
You have to set the fileUpload param to true !
$facebook = new Facebook(array(
'appId' => $facebookapi_id,
'secret' => $facebookapi_secret,
'fileUpload' => true,
'cookie' => true
));
Facebook have intentionally transformed the POST fields to a GET string using http_build_query() to stop fields beginning with # being used to accidentally or maliciously to upload files. Here's the GitHub issue.
A quick fix for this is to remove the http_build_query() from src/facebook.php in the SDK:
$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
Becomes:
$opts[CURLOPT_POSTFIELDS] = $params;
However if you do this you should take action to filter user generated messages that start with #. For example you could add a space to the front of each message.
If you use graph api to upload the photo it will getting the error (#200) User must have accepted TOS.
However if you use old rest api, just changing the url to https://api.facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX if you use above example.

Categories