update Twitter with tmhOAuth - php

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

Related

tweet post Image with {"errors":[{"code":189,"message":"Error creating status."}]}

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

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.

PHP - upload photo to Twitter with 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

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....

Problem with posting to Facebook Page using PHP script as Page account

I am using the following PHP code to post to my page wall. it is okay I can post successfully as admin but I want to post to the wall using the page name (page account). how do I post using the page name or page id. I tried to find good resource to explain Facebook API function but i did not find good documentation for it.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}");
exit;
}
else {
echo "i am connected";
}
$attachment = array('message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
?>
I found the solution to the problem.
To post or to do any task as a page , you need an access token.
Here is my final code after the update, I want to share it with you because I found many people having difficulty with getting the correct access code for a page.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 123456789){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
?>
My Facebook OAuth is a little rusty, but I believe this will help:
http://bugs.developers.facebook.net/show_bug.cgi?id=10005
It is the exact bug you are describing, but I have no way of testing that it corrects the issue specifically.
This should help: http://developers.facebook.com/docs/reference/api/page/#feed
It looks like you need the manage_pages permission.

Categories