I'm trying to make a script to automatically post a picture to my three fan pages.
Here is the script:
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'fileUpload' => true,
));
if($_GET["postType"] == "picture"){$type = "picture";}
else {$type = "link";}
// Download the picture, if $type == picture.
if($type == "picture")
{
$tempFileName = $_SERVER['DOCUMENT_ROOT'].'/TemporaryFiles/';
$tempFileName .= uniqid().'_'.basename($_GET["pictureUrl"]);
// Check if content retrieval is successful :D
if($imgContent = #file_get_contents($_GET["pictureUrl"]))
{
#file_put_contents($tempFileName,$imgContent);
}
}
foreach($pageIDs as $index=>$item)
{
$fbconfig['pageid'] = $item;
$facebook->setFileUploadSupport(true);
if($type == "picture")
{
$args = array(
'access_token' => $pageAccessTokens[$index],
'message' => $_GET["message"],
'source' => '#' . realpath($tempFileName),
);
$post_id = $facebook->api("/" . $albumIDs[$index] . "/photos","post",$args); // Post made :)
}
else if($type == "link")
{
$args = array(
'access_token' => $pageAccessTokens[$index],
'message' => $_GET["message"],
'link' => $_GET["linkUrl"],
'picture' => "",
);
$post_id = $facebook->api("/$pageid/feed","post",$args); // Post made :)
}
}
if($type == "picture")
{
unlink($tempFileName);
}
It's throwing out the following error:
Uncaught OAuthException: (#324) Requires upload file thrown in
I've tried to debug it and have no clue what's wrong with it. If anyone could help, would be grateful.
You need to add photo_upload to your requested scopes to upload images.
array('scope' => 'user_status,publish_stream,user_photos','photo_upload')
put this in your code after the $ facebook-> api ('/ me');
$this->facebook->setFileUploadSupport(true);
Related
i am using tmhOauth library to post status to twitter.
why I got 403 when i try update_with_media with a hashtag inside it. below is full code.
session_start();
require 'tmhOAuth.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'MYKEY',
'consumer_secret' => 'MYSECRET',
'curl_ssl_verifypeer' => false
));
if(isset($_GET['oauth_verifier'])){
$tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
$code = $tmhOAuth->request(
'POST',
$tmhOAuth->url('oauth/access_token', ''),
array(
'oauth_verifier' => $_GET['oauth_verifier']
)
);
if($code == 200){
$_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
$tmhOAuth->config['user_token'] = $_SESSION['access_token']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
$base64_data = file_get_contents($_SESSION['twitter_image_data']);
$status = <<<EOD
You can see yourself at Catholic U, too! Go to somelink.com to check out this cool app!
#hashtag
EOD;
echo strlen($status);
$params = array(
'media[]' => "{$base64_data};type=image/jpeg;filename=myimage.jpg",
'status' => $status
);
$response = $tmhOAuth->user_request(array(
'method' => 'POST',
'url' => $tmhOAuth->url("1.1/statuses/update_with_media"),
'params' => $params,
'multipart' => true
));
print_r($response);
if($response == 200){
?>
<pre>Your image has been uploaded to twitter. this window will be closed in 3 seconds...</pre>
<script>
setTimeout(function(){
window.close();
}, 3000);
</script>
<?php
}
unset($_SESSION['oauth']);
unset($_SESSION['twitter_image_data']);
}
}
anyway to debug it? $response only return response number. please help
You are having a 403 error which happens when accessing the page or resource you were trying to reach is forbidden for some reason. You should check for url errors and make sure you're specifying an actual web page file name and extension, not just a directory.
I can add photo to my twitter with API, and its not working
This is my code, and this code not working
<?php
require ('twit/tmhOAuth.php');
require ('twit/twitUnti.php');
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'XXX',
'consumer_secret' => 'XXX',
'user_token' => "XX",
'user_secret' => "XXX",
));
$image = "yeah.jpg";
$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "#{$image}",
'status' => "Don't slip up" // Don't give up..
),
true, // use auth
true // multipart
);
if ($code == 200) {
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
tmhUtilities::pr($tmhOAuth->response['response']);
}
?>
please help me, thanks
I am using this code to post to the user's wall :
require 'fb/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'redirect_uri' => 'urlhere',
);
$fb_session = $facebook->getUser();
// Session based API call.
if ($fb_session)
{
try
{
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
}
catch (Exception $e)
{
$fb_login_url = $facebook->getLoginUrl($params);
echo $e->getMessage();
}
}
else
{
$fb_login_url = $facebook->getLoginUrl($params);
}
The code is working till the point uuer clicks on login part to post. After that the url contains a code = xxxzz and a state =yyy but the $fb_session is 0.
This works sometimes without any changes. Please help!
You should include the access token when you try to post to some User Facebook wall.
First get access token:
$fbToken = NULL;
$fbToken = $facebook->getAccessToken();
right after $fb_session = $facebook->getUser();
and then include it in the $args array:
'access_token' => $fbToken
if session is NOT created or access token is not valid, you should redirect the user back to re-authorize your Facebook App.
So in ELSE, you should add
header("Location: $fb_login_url ");
after $fb_login_url = $facebook->getLoginUrl($params);
assuming that everything else is correct, but the access_token is not valid, you should also include header("Location: $fb_login_url "); before echo $e->getMessage();
in both cases you are using getloginurl
shouldn't it be
if ($fb_session) {
try {
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
} catch (Exception $e) {
$fb_logout_url = $facebook->getLogoutUrl;
.
.
.
I'm trying to upload image from a Facebook app. I get the following error: Fatal error: Uncaught CurlException: 26: failed creating formpost data thrown in /storage/content/00/103300/gjutveckling.se/public_html/fb/src/base_facebook.php on line 886
This is my code. The strange thing is that i got it to work, but then all if the sudden it stopped working. Can this be a SSL related problem, I just got a new one? Any suggestions must appreciated!
<?php
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'fileUpload' => true,
'cookie' => true,));
$facebook->setFileUploadSupport(true);
$FILE_PATH = 'imdage_' . $user_id . '.jpg';
$args = array('message' => 'message');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
?>
I tried this aswell, it doesn't give me an error, but no photo is uploaded.
<?php
function upload_to_facebook($image_data, $access_token)
{
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$facebook->setFileUploadSupport(true);
$albums = $facebook->api('/me/albums', 'get', array('access_token' => $access_token));
foreach ($albums[data] as $album)
{
if ($album['name'] == $image_data['album_name'])
{
$album_id = $album['id'];
}
}
if (!$album_id)
{
//Create an album
$album_details = array(
'message' => 'Album by FB APP',
'access_token' => $access_token,
'name' => $image_data['album_name']
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you’ve just created
$album_id = $create_album['id'];
}
//Upload a photo to album of ID…
$photo_details = array(
'message' => 'test image',
'access_token' => $access_token,
);
$photo_details['skrivreglerny.png'] = '#' . realpath($image_data['file']);
$upload_photo = $facebook->api('/'.$album_id.'/photos', 'post', $photo_details);
return $upload_photo;
}
?>
Try this please
$photo_details = array(
'message' => 'message',
'access_token' => 'USER ACCESS TOKEN',
);
$photo_details['image'] = '#'.realpath($image_data['file']);
$upload_photo = $facebook->api(('/me/photos', 'post', $photo_details);
see this link
http://pramodsivadas.co.in/2011/11/upload-an-image-to-a-facebook-album-facebook-php-sdk/
Using the php graph api I want to add picture to an event. So I m looking for some examples for the new graph api !
I'm sorry if the question is a bit unclear. Creating an event facebook use standard pictures of 31
calendar icon of facebook
http://i.stack.imgur.com/w7tMp.png in monocolor and color
I ( and many other person ) want to replace this picture using php as you can do it via the facebook page.
All descriptions in the internet fail.
I use the following code
$name= $parameter['betreff'];
$beschreibung=$parameter['thema'];
start_time = $parameter['anfangsdatum'] . " " . $parameter['anfangszeit'];
$ort="" . $parameter['ort'];
$picture='http://www.tvwehen.de/facebook/facebook_plan_event/news.png';
$privacy="CLOSED";
$event_param = array(
'access_token' => $access_token,
'page_id' =>$fanPageId,
'name' => $name,
'start_time' => $start_time,
'description' => $beschreibung,
'location' => $ort,
'pic_big' => $picture,
'pic_small' => $picture,
'logo.png' => $picture,
'privacy' => $privacy
);
$event_id = $facebook->api($fanPageId . "/events", "POST", $event_param);
The event is created on the fanpage but without any picture ! Why ?
Your question is a bit unclear, but assuming I've understood you correctly, you simply want a users profile picture? That can be found here:
http://graph.facebook.com/username/picture
So mine would be at http://graph.facebook.com/filip.soderholm/picture
You may also use the userId instead of the username if you have that.
You can get help from this address: https://gist.github.com/1178154
<?php
/**
* Implementation of hook_nodeapi
*/
function hps_the_hook_nodeapi (&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->type == 'event') {
if ($op == 'insert') {
dpm($node);
//require_once(drupal_get_path('module', 'hps_the_hook') . '/lib/facebook-php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => '193560340711521',
'secret' => 'secret',
'fileUpload' => true,
));
$page_id = "161330440592966";
$location = $node->field_location[0]['value'];
$from_date = $node->field_date[0]['value'];
$to_date = $node->field_date[0]['value2'];
$image_path = $node->field_image[0]['filepath'];
$event_param = array(
"access_token" => 'access_token',
"name" => $node->title,
"start_time" => strtotime($from_date),
"end_time" => strtotime($to_date),
"location" => $location,
"page_id" => $page_id,
"picture" => "#".realpath($image_path),
"source" => "#".realpath($image_path),
);
$facebook->setFileUploadSupport(true);
try {
dpm($event_param);
$event_id = $facebook->api("/161330440592966/events", "POST", $event_param);
dpm($event_id);
}
catch (Exception $ex) {
dpm($ex);
}
}
}
}
?>