Facebook Graph API - Photo Upload - facebook

I have an application that uses the old Facebook API but now I'm migrating it. The application works good until I try to upload a photo.
I knew how to do it in the old way, but now... I'm in troubles.
This is the way I used to do it:
$args = array
(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML',
'caption' => $caption
);
signRequest($args, $sec);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.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);
Any ideas??
Thanks

The API url starts with https:// not http://. That could be the issue.

I found the solution here:
Uploading a picture to facebook
There is shown how to use the new Facebook Graph API with the PHP Curl function and a valid session token.

Related

POST data to AtTask's API?

I'm using AtTask's API with PHP and cURL.
Is there a way to POST data instead of appending it to the end of the URL with a question mark?
I know that I can change the request name itself like CURLOPT_CUSTOMREQUEST => 'POST' and I tried adding CURLOPT_POST => true
However, the CURLOPT_POSTFIELDS => array('name' => 'Untitled Project') is still ignored.
Did anyone work with this?
Pretty sure you need to wrap your postfields array with http_build_query(). Here is an example for logging in that works for me (when I put in my username and password):
$URL = 'https://hub.attask.com/attask/api/login';
$username = 'admin';
$password = 'user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'username'=>$username,
'password'=>$password
)));
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
Hope that helps.
I just ran into the same problem you did. It's been a few months since you asked the question, but if you're still running into it (or for anyone else hitting this wall), the issue is that you need to set the proper CURL options for POST/GET.
For a GET you'll need to set CURLOPT_HTTPGET to "true". It just makes sure the headers are in the correct order for the AtTask API server.
I've just created a github repo for the changes I've made to their sample StreamClient class.
https://github.com/angrychimp/php-attask
Feel free to use that or just lift the code from it. For your login example, GreenJimmy's example is 100% accurate. For your search question, you'll need to do something like the following.
$URL = 'https://hub.attask.com/attask/api/v4.0/';
$username = 'admin';
$password = 'pass';
$URL .= "task/search/?sessionID=$sessionID&ID=$taskID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
The StreamClient in my github repo allows for arrays to be used for search params and response fields. It does make things a lot easier. For example.
require_once('StreamClient.php');
$client = new StreamClient('https://hub.attask.com', '4.0');
$client->login('admin', 'pass');
$records = $client->search('task', array('ID' => $taskID), array('assignedToID:emailAddr','actualWork'));
var_dump($records);

How to post to Facebook page as page itself

I am developing a blog module. When the admin add a new blog post, a new post will be made in the facebook wall of a page(Let's call it Mypage). The code for it is:
$_accessKey = FB_ACCESS_KEY;
$_feedId = FB_FEED_ID;
$attachment = array(
'access_token' => $_accessKey,
'message' => $postData['message'],
'name' => $postData['name'],
'description' => $postData['caption']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$_feedId.'/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close ($ch);
In the above code:
FB_ACCESS_KEY is the access token of the App from an user Test
FB_FEED_ID is the feed id of the page.
Now the posting to the Mypage is working, but the posts are coming as Added by Test.
How can I make the post as if it was made by the Mypage itself?
Test is an administrator of the page Mypage.
Authenticating as a Page should solve the issue for you: https://developers.facebook.com/docs/authentication/pages/

How to publish a photo to a Facebook page as a separate story

If I use the Facebook API to publish a photo to a page they appear as single story.
$args = array(
'message' => '',
);
$args["picture"] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
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);
If I publish from Facebook they appear correctly as a single story for each picture.
How to do this programmatically?

facebook, post comment via php curl

From one tutorials here:
Post a reply on a comment in Facebook using cURL PHP / Graph API
I tried to post comment to after one post, But return:
[type] => OAuthException [message] => (#200) The user hasn't authorized the application to perform this action
This is not my app acount wall's post id, I tried to my freinds wall, he have allowed my app, I can post a post to his wall, but failed in a comment, I got the post id via
https://graph.facebook.com/<his fid>/feed?access_token=140XXXXXXXXXXX, so the post id has no problem.
What steps have I missed?
$fbId = '100001102789652_233997699980321';
$accessToken = '140XXXXXXXXXXXX';
$url = "https://graph.facebook.com/{$fbId}/comments";
$attachment = array(
'access_token' => $accessToken,
'message' => "Hi comment",
);
// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$comment = curl_exec($ch);
curl_close ($ch);
$comment = json_decode($comment, TRUE);
print_r($comment);
?>
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
should be
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($attachment));
as you can't post an array

Add a wall post to a page or application wall as page or application with facebook graph API

I wan't to create a new wall post on a appliaction page or a "normal" page with the facebook graph API. Is there a way to "post as page"? With the old REST-API it worked like this:
$facebook->api_client->stream_publish($message, NULL, $links, $targetPageId, $asPageId);
So, if I passed equal IDs for $targetPageId and $asPageId I was able to post a "real" wall post not caused by my own facebook account.
Thanks!
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
$args = array(
'access_token' => $page_access_token,
'message' => "I'm posting as a Page!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
To publish as Page you need to add manage_pages permission first of all (and get the tokens).
Next use something like this:
$url = 'https://api.facebook.com/method/stream.publish?message=TEST&target_id=PAGEID&uid=PAGEID&access_token=YOUR_TOKEN';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
Set the value of targetpageid=null and check the output...