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....
Related
I'm trying to publish tweet on twitter with image and a link to my site
I get the following error:
{"errors": [{"code": 189, "message": ". Error creating status"}]}
I tried with twitter api:
with update.json
the tweet is published but no picture.
and
update_with_media.json
I get error
this is the code I use:
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxx',
'user_token' => 'xxxxxxxxxxxxxxxxxxxx',
'user_secret' => 'xxxxxxxxxxxxxxxxxx',
));
// we're using a hardcoded image path here. You can easily replace this with an uploaded image-see images.php example)
// 'image = "#{$_FILES['image']['tmp_name']};type={$_FILES['image'] ['type']};filename={$_FILES['image']['name']}",
$picimg="img.jpg";
$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
array(
'media[]' => "#{$picimg}",
'status' => "status"
),
true, // use auth
true // multipart
);
if ($code == 200) {
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
tmhUtilities::pr($tmhOAuth->response['response']);
}
I searched a lot on google but not served me anything.
I do not understand what's going on
It was poorly cast the image url and more stuff.
I have made an example of different api in my local.
I used APIs
https://github.com/themattharris/tmhOAuth
I've also installed composer for dependencies.
installing dependencies twitter api.
Well the most important thing is:
this piece of code:
$picimg="img.jpg";
array(
'media[]' => "#{$picimg}",
'status' => "status"
)
this is wrong.
but this above is good
$image = __DIR__.DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . 'fotos'. DIRECTORY_SEPARATOR . 'foto1.jpg';
DIRECTORY_SEPARATOR is used instead of backslash, and this is very important if not the error response would be 189
$name = basename($image);
$status = "Picture time";
$media = "#{$image};type=image/jpg;filename={$name}";
media must be represented well in this format, not only url string.
// we set the type and filename are set here as well
$params = array(
'media[]' => $media,
'status' => $status
);
This is working AMIGOS
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'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);
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']);