I am using matt harris' twitter library https://github.com/themattharris/tmhOAuth and following the image upload example.
I get a zero returned when trying to post an image with no $tmhOAuth->response['response'] being returned.
Steps i have tried
Running the example as it is- fails
Running verify ssl- works fine
Running the status update with out the image- it posts correctly as expected
swapping the url from 1 to 1.1- nothing changes, library still returns zero
It runs quite quickly implying its not even trying to post an image.
Any ideas on why this isnt working or anything i need to configure on the server
Below is the code i copied from the example to try with.
<?php
// testing hmac works- correctly
echo hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
$tmhOAuth = array( 'consumer_key' => 'removed',
'consumer_secret' => 'removed',
'user_token' => 'removed',
'user_secret' => 'removed');
// array(
// 'consumer_key' => 'YOUR_CONSUMER_KEY',
// 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
// 'user_token' => 'A_USER_TOKEN',
// 'user_secret' => 'A_USER_SECRET',
// )
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth($tmhOAuth);
// we're using a hardcoded image path here. You can easily replace this with
// an uploaded image - see images.php in the examples folder for how to do this
// 'image = "#{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
// this is the jpeg file to upload. It should be in the same directory as this file.
$image = 'image.png';
$code = $tmhOAuth->request(
'POST',
'https://upload.twitter.com/1.1/statuses/update_with_media.json',
array(
'media[]' => "#{$image};type=image/jpeg;filename={$image}",
'status' => 'Picture time',
),
true, // use auth
true // multipart
);
if ($code == 200) {
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
tmhUtilities::pr($tmhOAuth->response['response']);
}
?>
The post url https://upload.twitter.com in the request should be http://api.twitter.com
Related
I'm using the library tmhOAuth to post to Twitter in an app and I've already implemented uploading pictures but am having trouble implementing video upload.
This is the call I use to upload pictures and works perfectly with images.
$temp = '#upload/'.$name.';type='.$_FILES['img']['type'].';filename='.$name;
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json', array('media' => $temp), true, true);
So I thought it might be the same for videos but I got the error
stdClass Object ( [request] => /1.1/media/upload.json [error] => media type unrecognized. )
I believe I have to make 3 separate calls, as per the Twitter API, so I tried this
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json?command=INIT&media_type=video/mp4&total_bytes='.$_FILES['img']['size'], array('media' => $temp), true, true);
$media_id = json_decode($tmhOAuth->response['response'])->media_id_string;
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id='.$media_id.'&segment_index=0', array('media' => $temp), true, true);
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id='.$media_id, array('media' => $temp), true, true);
but I kept getting the same error for all 3 calls
stdClass Object ( [request] => /1.1/media/upload.json [error] => media type unrecognized. )
Can anyone provide an example as to how to upload videos to twitter? I could find no examples online and it might just not be possible.
I had the same problem. Here's how I managed to solve it.
First you set up a var containing the filesystem full path to the media you want to upload.
$media_path = '/PATH/TO/THE/file.mp4';
Then instantiate $tmhOAuth and do the 3 steps :
$tmhOAuthUpload = new tmhOAuth();
INIT:
$code = $tmhOAuthUpload->request(
'POST',
$tmhOAuthUpload->url('/1.1/media/upload.json'),
array(
"command" => "INIT",
"total_bytes" => (int)filesize($media_path),
'media_type' => 'video/mp4',
)
);
Retrieve media id returned by Twitter
$results = json_decode($tmhOAuthUpload->response['response']);
$media_id = $results->media_id_string;
APPEND: Handle video/media upload with the Append loop
$fp = fopen($media_path, 'r');
$segment_id = 0;
while (! feof($fp)) {
$chunk = fread($fp, 1048576); // 1MB per chunk for this sample
$tmhOAuthUpload->request(
'POST',
$tmhOAuthUpload->url('/1.1/media/upload.json'),
array(
"command" => "APPEND",
"media_id" => $media_id,
'media_data' => base64_encode($chunk),
"segment_index" => $segment_id
)
);
$segment_id++;
}
FINALIZE:
$tmhOAuthUpload->request(
'POST',
$tmhOAuthUpload->url('/1.1/media/upload.json'),
array(
"command" => "FINALIZE",
"media_id" => $media_id,
)
);
By then I was able to to send my tweet:
$code = $tmhOAuth->request(
'POST',
$tmhOAuthUpload->url('1.1/statuses/update'),
array(
'media_ids' => $media_id,
'status' => $text,
),
true // use auth
);
Hope that helps
I've only been able to get video uploading working with CodeBird - a different PHP library.
The Twitter API calls for video are quite different from uploading images, as you've discovered.
Uploading videos to Twitter (≤ 15MB, MP4) requires you to send them in chunks. You need to perform at least 3 calls to obtain your media_id for the video:
Send an INIT event to get a media_id draft.
Upload your chunks with APPEND events, each one up to 5MB in size.
Send a FINALIZE event to convert the draft to a ready-to-tweet media_id.
Post your tweet with video attached.
Remember, each APPEND must be 5MB or under.
If you are consistently getting "Media Type Unrecognised" errors, it might be that the video you are using is incompatible with Twitter. Can you test uploading the video via another service?
Thank you very much for that answer Pierre! I was however getting a “Not valid video” error if I tried to create the tweet too soon. The video wasn't done being processed by Twitter. In addition to Pierre's code, I needed something like this to check STATUS, after FINALIZE:
$videoCount = 0;
do
{
$tmhOAuth->request(
'GET',
$tmhOAuth->url('/1.1/media/upload.json'),
array(
"command" => "STATUS",
"media_id" => $mediaID,
)
);
$twitterResult = json_decode($tmhOAuth->response['response']);
if ($twitterResult->processing_info->state != 'succeeded')
{ sleep(5); }
$videoCount++;
}
while ($twitterResult->processing_info->state != 'succeeded' && $videoCount < 5);
Note: my variable names are different
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 build a small twitter app demo. Everything works fine like retrieving tweets, making searches etc. But when I try to retrieve trending topics using woeid, I get a 404 - This page does not exist error. I have tried different woeids'.
I will be grateful if someone could point what I am doing wrong.
here is my code.
public function trends1(){
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => $this->consumerkey,
'consumer_secret' => $this->consumersecret,
'user_token' => $this->accesstoken,
'user_secret' => $this->accesstokensecret,
'curl_ssl_verifypeer' => false
));
$args = array('id' => '23424975');
$code = $tmhOAuth->request("GET", $tmhOAuth->url("https://api.twitter.com/1.1/trends/place.json"), $args);
print $code;
$res = $tmhOAuth->response['response'];
$trends = json_decode($res, true);
return $trends;
}
Try removing https://api.twitter.com/ from the URL. I was getting a 404 as well until I removed that segment of the URL and am now getting a 200 response code.
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....
I had a look at several stackoverflow posts about this error message, but none of them worked for me.
I want to upload a photo to facebook:
public function uploadPhoto($path){
$photoSettings = array(
'access_token'=> $this->facebook->getAccessToken(),
'name' => 'uploaded foto',
'source' => '#' . realpath($path)
);
$photo = $this->facebook->api('me/photos','POST',$photoSettings);
}
When i call this function, i get the following error message:
Uncaught CurlException: 26: failed creating formpost data
I am 100% sure that the image i want to upload exists (and the path is correct).
This is my facebook initialization: (fileUpload is true)
$this->facebook = new Facebook(array(
'appId' => $this->config['appId'],
'secret' => $this->config['appSecret'],
'fileUpload' => true,
'cookie' => true
));
I really don't understand why i get that error because my code seems to be correct. Do you think there could be a problem with my server / the server's cURL configuration? I dont know much about cURL.
I hope you can help me! I am looking forward for your answers :-)
Greetings,
Andreas
Your realpath($path) is not pointing to the actual server image location. If $path is the complete path of the image, then use 'source' => '#' . $path instead.
I kept getting “CurlException: 26: failed creating formpost data”
Here is my working code for uploading a photo from the same directory as the PHP page communicating with Facebook:
$facebook = new Facebook(array(
'appId' =>'*****',
'secret' =>'*******',
'fileUpload' => true,
'cookie' => true
));
$user = $facebook ->getUser();
if($user)
{
$facebook->setFileUploadSupport(true);
$args = array(
'message' => 'TEst from App.',
'image' => '#' . realpath('awesome.jpeg')
);
try {
$data = $facebook->api('/me/photos', 'post', $args);
} catch(FacebookApiException $e) {
echo "ERROR: " . $e;
}
}