i am using codebird-php to post images on twitter, when i do that i get 200 ok http code but the image is not uploaded. Here is my code:
<?php
session_start();
require_once ('./src/codebird.php');
\Codebird\Codebird::setConsumerKey('74AFitlDilqB2HlFQ8Cjszz6I', 'tDlVndY7iJG8loFGG1sq3gJaj59CwNx6UV5o6wEtV0LJebNJ0y'); // static, see 'Using multiple Codebird instances'
$cb = \Codebird\Codebird::getInstance();
$access_token = $_SESSION['access_token'];
$cb->setToken($access_token['oauth_token'], $access_token['oauth_token_secret']);
//$reply = $cb->statuses_update('status=Whohoo, I just again tweeted!');
// send tweet with these medias
$reply = $cb->media_upload(array(
'media' => 'http://www.bing.com/az/hprichbg/rb/BilbaoGuggenheim_EN-US11232447099_1366x768.jpg'
));
print_r($reply);
?>
This is what i am getting on running it in my browser:
stdClass Object ( [media_id] => 540134777223790592 [media_id_string] => 540134777223790592 [size] => 179801 [image] => stdClass Object ( [w] => 1366 [h] => 768 [image_type] => image/jpeg ) [httpstatus] => 200 [rate] => )
PS: I am running it on localhost, tweeting text works but not image and i am using Abrahams oAuth for getting oAuth token.
i fixed it by changing
$reply = $cb->media_upload(array(
'media' => 'http://www.bing.com/az/hprichbg/rb/BilbaoGuggenheim_EN-US11232447099_1366x768.jpg'
));
to
$params = array(
'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter',
'media[]' => 'http://www.bing.com/az/hprichbg/rb/BilbaoGuggenheim_EN-US11232447099_1366x768.jpg'
);
// send tweet with these medias
/*$reply = $cb->media_upload(array(
'media[]' => "#http://www.bing.com/az/hprichbg/rb/BilbaoGuggenheim_EN-US11232447099_1366x768.jpg"
));*/
$reply = $cb->statuses_updateWithMedia($params);
Related
I need to download (by php: curl, file_get_contents...) the post full picture to site folders.
I can get the post item data, even the full_picture url but when I want to download it by wget or curl the server responds with an 403.
If I try to download that url by browser, no problem, I can see it.
https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2
But if I try from curl or wget I get a 401 response
wget https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2
[1] 9988
[2] 9989
$ converted 'https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457' (ANSI_X3.4-1968) -> 'https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457' (UTF-8)
--2016-01-22 15:20:39-- https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457
Resolving scontent.xx.fbcdn.net (scontent.xx.fbcdn.net)... 31.13.83.4, 31.13.83.4
Connecting to scontent.xx.fbcdn.net (scontent.xx.fbcdn.net)|31.13.83.4|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2016-01-22 15:20:39 ERROR 403: Forbidden.
I've tried also appending the access token to the path:
...&access_token=XXXXXXXX
This is the json post item:
stdClass Object
(
[id] => 396231587217143_531401453700155
[created_time] => 2016-01-11T13:21:37+0000
...
[picture] => https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s130x130/12525704_987920591279679_1409522552_n.jpg?oh=34e4a1b419bb842c5f84441f9a781745&oe=574676AE
[full_picture] => https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2
[attachments] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[media] => stdClass Object
(
[image] => stdClass Object
(
[height] => 405
[src] => https://scontent.xx.fbcdn.net/hvthumb-xta1/v/t15.0-10/s720x720/12525704_987920591279679_1409522552_n.jpg?oh=33b2b223cc3e6c2ddd1a94f098c44457&oe=5748C6D2
[width] => 720
)
)
[target] => stdClass Object
(
[id] => 10153413586308403
[url] => https://www.facebook.com/WorldArchery/videos/10153413586308403/
)
[type] => video_inline
[url] => https://www.facebook.com/WorldArchery/videos/10153413586308403/
)
)
)
...
)
I'm getting post information from a facebook page this way.
Init the Facebook class:
$this->facebook = new Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
//'default_access_token' => '{access-token}', // optional
]);
Getting post data:
...
$fields = array(
'id',
'application',
'call_to_action',
'caption',
'created_time',
'description',
'from',
'icon',
'is_hidden',
'is_published',
'link',
'message',
'message_tags',
'name',
'object_id',
'picture',
'full_picture',
'place',
'privacy',
'properties',
'source',
'status_type',
'story',
'story_tags',
'targeting',
'to',
'type',
'updated_time',
'with_tags',
'actions',
// 'tags',
// 'object_attachment',
'feed_targeting',
'attachments',
);
$data = $this->facebook->get($id . '?fields=' . implode(',', $fields));
$item = json_decode($data->getBody());
$object (object) $item;
Ty to set the UA like #CBroe mentioned:
wget -U Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36 {your-url}
You may consider using cURL as an alternative:
Saving image from PHP URL
I am trying to use codebird to tweet using PHP. Initially I was unable to get Access Token but after I defined CallbackURL in settings that issue seems to be resolved. Now it is returning oauth token:
Codebird\Codebird Object ( [_oauth_token:protected] => codehere [_oauth_token_secret:protected] => codehere [_return_format:protected] => 0 [_supported_media_files:protected] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [_version:protected] => 3.0.0-dev [_use_curl:protected] => 1 [_timeout:protected] => 10000 [_connectionTimeout:protected] => 3000 )
But when i try to tweet i get following error:
stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [code]
=> 89 [message] => Invalid or expired token. ) ) [httpstatus] => 401 [rate] => )
Following is my code
Codebird\Codebird::setConsumerKey('copy+paste from twitter', 'copy+paste from twitter'); // I changed it to my settings
$cb = \Codebird\Codebird::getInstance();
if (! isset($_SESSION['oauth_token'])) {
// get the request token
$reply = $cb->oauth_requestToken(array(
'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
));
// store the token
$cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
$_SESSION['oauth_verify'] = true;
// redirect to auth website
$auth_url = $cb->oauth_authorize();
header('Location: ' . $auth_url);
die();
} elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
// verify the token
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
unset($_SESSION['oauth_verify']);
// get the access token
$reply = $cb->oauth_accessToken(array(
'oauth_verifier' => $_GET['oauth_verifier']
));
// store the token (which is different from the request token!)
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
// send to same URL, without oauth GET parameters
header('Location: ' . basename(__FILE__));
die();
}
// assign access token on each page load
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
print_r($cb);
$params = array(
'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter'
);
$reply = $cb->statuses_update($params);
print_r($reply);
Thanks in advance for the assistance.
Is your callback address the one registered with Twitter in the app definition?
Do you have "read and write" access in the app definition?
Have you exceeded the rate limit for posting?
I'd check those things first, as I don't see anything obviously missing from your code snippet.
Facebook Login URL Code :
$this->set("facebookUrl",$this->facebook->getLoginUrl(
array(
'scope' => 'email,publish_actions',
'redirect_uri' =>"http://redirected_url"
'display'=>"popup"
)
));
Post story on facebook code :
$params = array(
"access_token" => "access_token_of_logged_in_user",
"message" => "Testing facebook post",
"picture" => "http://i.stack.imgur.com/Og6yH.jpg?s=128&g=1",
"caption" => "www.pontikis.net",
"description" => "Description of post."
);
$myProfile = $this->facebook->api('/me');
$permissions = $this->facebook->api('/me/permissions');
pr($permissions);
try {
$ret = $this->facebook->api('/'.$myProfile['id'].'/feed', 'POST', $params);
echo 'Successfully posted to Facebook';
}
catch(Exception $e) {
echo $e->getMessage();
}
Permission Displayed here :
Array
(
[data] => Array
(
[0] => Array
(
[permission] => public_profile
[status] => granted
)
[1] => Array
(
[permission] => email
[status] => granted
)
)
)
I got following Error when I post on facebook :
(#200) The user hasn't authorized the application to perform this action
If you're using Graph API >=v2.0, then you need to pass Login Review for non admin/tester/developer users before you can use this permission in your app:
See
https://developers.facebook.com/docs/apps/review/login#do-you-need-review
I'm trying to post a photo using the php-sdk - all was working for months successfully but all of the sudden no dice.
Other functions are still working with the same code base (ie: posting messages to wall) - its just the posting of photos that broke on my side.
try {
$data = $facebook->api('/me/photos', 'post', $args);
} catch (FacebookApiException $e) {
print_r($e);}
Is dumping:
FacebookApiException Object ( [result:protected] => Array ( [error_code] => 3 [error] => Array ( [message] => No URL set! [type] => CurlException ) ) [message:protected] => No URL set! [string:private] => [code:protected] => 3 [file:protected] => /locationofmy/base_facebook.php [line:protected] => 818 [trace:private] => Array ( [0] => Array [..............]
From the FB php-sdk lines 818:
if ($result === false) {
$e = new FacebookApiException(array(
'error_code' => curl_errno($ch),
'error' => array(
'message' => curl_error($ch),
'type' => 'CurlException',
),
));
curl_close($ch);
throw $e;
}
curl_close($ch);
return $result;
}
This was working for a long time - has something changed on Facebooks side?
EDIT: php-sdk version: 3.1.1
EDIT 2:
$tag = array(
'tag_uid' => 'acct_num',
'x' => 0,
'y' => 0
);
$tags[] = $tag;
$args = array(
'message' => $item_description,
'image' => '#' . realpath($temp_path . $tempFile),
'tags' => $tags,
);
Probably that the file doesnt exist, or the file system can't serve it anymore. Can you confirm "$temp_path . $tempFile" - the error is no URL, usually that means no real path to image. I suspect, that the images are missing and/or your servers filled up and no local images are saving. (Yes, this has happened to me before!)
Try changing the image to source. I believe this should fix your issue.
The Facebook API requires a source field but I did not see anything about an image field.
You may also have to pass the actual file contents instead of the real_path (based on the example). Or, alternatively, pass an external URL (based on my understanding of the documentation).
Source: https://developers.facebook.com/docs/reference/api/photo/
Example: https://developers.facebook.com/blog/post/498/
I am working on an application that is basically going to operate in a Kiosk, the point is to allow users while they are at a business to be able to login to facebook and after logging in it posts a message saying they are there, afterwords they are given a coupon.
The problem has arisen that after they have logged in and then logged out, the next person logs in with their account ends up posting as the previous user, this continues adnauseum.
After getting their coupon the script automatically logs them out after 15 seconds and returns the application to the home screen for the next user. When they login, which they are able to do it returns them to the page asking for permission to post, but it is pulling all of the previous users information. This is the code being called in the page after being sent to logging in on facebook.
<?php
//include the Facebook PHP SDK
include_once 'couponGenerator/facebook.php';
//start the session if necessary
if( session_id() ) {
} else {
session_start();
}
//instantiate the Facebook library with the APP ID and APP SECRET
$facebook = new Facebook(array(
'appId' => '00000000000',
'secret' => '000000000000000000000',
'cookie' => true,
'status' => true,
'oath' => true
));
$access_token = $facebook->getAccessToken();
$_SESSION['active'][$access_token];
//get the news feed of the active page using the page's access token
$page_feed = $facebook->api(
'/me/feed',
'GET',
array(
'access_token' => $_SESSION['active']['access_token']
)
);
$fbuser = $facebook->api('/me');
//var_dump($page_feed); exit;
?>
I have attempted on the homepage of of deleting facebook cookies and sessions and this has not solved anything, I am just trying to figure out what I am doing wrong and any advice would be very welcome.
$facebook->destroySession();
$facebook->_killFacebookCookies();
public function _killFacebookCookies()
{
// get your api key
$apiKey = $this->getAppId();
// get name of the cookie
$cookie = $this->getSignedRequestCookieName();
$cookies = array('user', 'session_key', 'expires', 'ss');
foreach ($cookies as $name)
{
setcookie($apiKey . '_' . $name, false, time() - 3600);
unset($_COOKIE[$apiKey . '_' . $name]);
}
setcookie($apiKey, false, time() - 3600);
unset($_COOKIE[$apiKey]);
$this->clearAllPersistentData();
}
Here is the updated connection class
`
<?php
//include the Facebook PHP SDK
include_once 'facebook.php';
//instantiate the Facebook library with the APP ID and APP SECRET
$facebook = new Facebook(array(
'appId' => '122628977190080',
'secret' => '123123123123123123123123',
'cookie' => true
));
$access_token = $facebook->getAccessToken();
unset ($_SESSION['active'][$access_token]);
session_unregister ($_SESSION['active'][$access_token]);
//Get the FB UID of the currently logged in user
$user = $facebook->getUser();
//if the user has already allowed the application, you'll be able to get his/her FB UID
if($user) {
//start the session if needed
if( session_id() ) {
} else {
session_start();
}
//do stuff when already logged in
//get the user's access token
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions',
'GET',
array(
'access_token' => $access_token
)
);
//check if the permissions we need have been allowed by the user
//if not then redirect them again to facebook's permissions page
$permissions_needed = array('publish_stream', 'email');
foreach($permissions_needed as $perm) {
if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 ) {
$login_url_params = array(
'scope' => 'publish_stream,email',
'fbconnect' => 1,
'display' => "page",
'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
//if the user has allowed all the permissions we need,
//get the information about the pages that he or she managers
$accounts = $facebook->api(
'/me/accounts',
'GET',
array(
'access_token' => $access_token
)
);
//save the information inside the session
$_SESSION['access_token'] = $access_token;
$_SESSION['accounts'] = $accounts['data'];
//save the first page as the default active page
$_SESSION['active'] = $accounts['data'][0];
//redirect to manage.php
header('Location: ../facebook_result.php');
} else {
//if not, let's redirect to the ALLOW page so we can get access
//Create a login URL using the Facebook library's getLoginUrl() method
$login_url_params = array(
'scope' => 'read_stream,email',
'fbconnect' => 1,
'display' => "page",
'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
exit();
}
?>`
After calling the logoff script, I am run this piece of code on the homepage to see if everything is set.
<?php
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
echo "$uid";
} catch (FacebookApiException $e) {
print_r($e);
}
?>
it gives me this result
FacebookApiException Object ( [result:protected] =>
Array ( [error] => Array ( [message] =>
An active access token must be used to query information about the current user.
[type] => OAuthException [code] => 2500 ) )
[message:protected] => An active access token must be
used to query information about the current user.
[string:private] => [code:protected] => 0 [file:protected] =>
/home/m3dev/public_html/couponsite/couponGenerator/base_facebook.php
[line:protected] => 1046 [trace:private] => Array ( [0] => Array ( [file] => /home/m3dev/public_html/couponsite/couponGenerator/base_facebook.php [line] => 751 [function] => throwAPIException [class] => BaseFacebook [type] => -> [args] => Array ( [0] => Array ( [error] => Array ( [message] => An active access token must be used to query information about the current user. [type] => OAuthException [code] => 2500 ) ) ) ) [1] => Array ( [function] => _graph [class] => BaseFacebook [type] => -> [args] => Array ( [0] => /me ) ) [2] => Array ( [file] => /home/m3dev/public_html/couponsite/couponGenerator/base_facebook.php [line] => 560 [function] => call_user_func_array [args] => Array ( [0] => Array ( [0] => Facebook Object ( [appId:protected] => 162628977190080 [apiSecret:protected] => **SECRET KEY REMOVED ** [user:protected] => 0 [signedRequest:protected] => Array ( [algorithm] => HMAC-SHA256 [code] => 961628b1ca0354544541d58e.1-34319949|p3D3pSNoawlC1wBllhiN7zoEpJY [issued_at] => 1331218933 [user_id] => 34319949 ) [state:protected] => [accessToken:protected] => 162628977190080|**SECRET KEY REMOVED** [fileUploadSupport:protected] => ) [1] => _graph ) [1] => Array ( [0] => /me ) ) ) [3] => Array ( [file] => /home/m3dev/public_html/couponsite/index.php [line] => 71 [function] => api [class] => BaseFacebook [type] => -> [args] => Array ( [0] => /me ) ) ) )
You may be destroying a Facebook session but you don't seem to be destroying your own session.
Clear out
$_SESSION['active'][$access_token];
You need to force Facebook Re-Authentication for each user.
I'm not sure if the PHP API you're using supports this, but the OAuth dialog can receive a auth_type that when valued to reauthenticate forces the user to provide his credentials:
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. '&auth_type=reauthenticate&auth_nonce=' . $auth_nonce;
This can be done useg the Javascript API as well.