PHP - upload photo to Twitter with PHP - php

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

Related

tmhOauth update_with_media using hashtag forbidden

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.

Error "Requires upload file thrown in" from Facebook Graph API

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);

Posting image to twitter using php not working

Posting image to twitter using php not working . Please check my code.
<?
define( 'YOUR_CONSUMER_KEY' , '######################');
define( 'YOUR_CONSUMER_SECRET' , '######################');
require ('../../twitt/tmhOAuth.php');
require ('../../twitt/tmhUtilities.php');
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => "######################",
'consumer_secret' => "######################",
'user_token' => "######################",
'user_secret' => "######################",
));
$image = 'Tulips.jpg';
$code = $tmhOAuth->request( 'POST','https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "#{$image};",
'status' => 'message text written here',
),
true, // use auth
true // multipart
);
if ($code == 200){
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
}else{
tmhUtilities::pr($tmhOAuth->response['response']);
}
print_r($code);
?>
It is not working. I always return 0. What is the issue in this code? Anyone please help me....

update Twitter with tmhOAuth

I want to update my Twitter. I use tmhOAuth from themattharris . I have enabled Curl and the Twitter app settings are correct with read write access. I checked the header and requests are empty. At first the code should work just changing the key and changes in Twitter. What do you think I'm doing wrong?
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
// 'consumer_key' => 'YOUR_CONSUMER_KEY',
//'consumer_secret' => 'YOUR_CONSUMER_SECRET',
//'user_token' => 'A_USER_TOKEN',
// 'user_secret' => 'A_USER_SECRET',
'consumer_key' => 's8wEiEo0EFk7fCX5jHfVfw',
'consumer_secret' => 'db2qENrjCRxtaOFs9a7wT7YPjWNRxO1yHHxT9EkM',
'user_token' => '92957047-suEq4sH6ADTFXfOqrWfpbX6D9OqYGMyKA8CMAQVz4',
'user_secret' => 'LqhbQRp29M7L5UfdtJhde3kHxzB5oOn5xtGIdy35bOY',
));
$code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
'status' => 'My Twitter Message'
));
if ($code == 200) {
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
tmhUtilities::pr($tmhOAuth->response['response']);

How to add profile image to event using the new php graph api

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);
}
}
}
}
?>

Categories