New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweet image is not working for me #76

Closed
silvestret opened this Issue Sep 12, 2012 · 18 comments

Comments

Projects
None yet
8 participants
@silvestret

silvestret commented Sep 12, 2012

function post_tweet($tweet_text,$image) {
    require_once('tmhoauth/tmhOAuth.php');
    require_once('tmhoauth/tmhUtilities.php');

    $connection = new tmhOAuth(array(
        'consumer_key' => 'CODE',
        'consumer_secret' => 'CODE',
        'user_token' => 'CODE',
        'user_secret' => 'CODE'
    )); 

    // Make the API call

    $image = 'imagenes/producto/'.$image;

    $code = $connection->request(
                        'POST',                         
                        'https://api.twitter.com/1.1/statuses/update_with_media.json',

// 'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "@{$image}",
'status' => $tweet_text
),
true, // use auth
true // multipart
);

    echo $connection->response['raw'];

    if($code == 200) {
        return tmhUtilities::pr(json_decode($connection->response['response']));
    } else {
        return tmhUtilities::pr($connection->response['response']);
    }



    //return $connection->response['response'];
}

I dont know if it is related with @ symbol but i got the next error
{"errors":[{"code":189,"message":"Error creating status"}]}

Thanks in advance

@themattharris

This comment has been minimized.

Owner

themattharris commented Sep 12, 2012

it looks like you haven't defined $tweet_text which means you are trying to tweet with no text. try defining $tweet_text with something you want to post with the Tweet and try again.

@ghost ghost assigned themattharris Sep 12, 2012

@silvestret

This comment has been minimized.

silvestret commented Sep 13, 2012

$tweet_text is defined like a parameter in the function

function post_tweet($tweet_text,$image) {

i have tried of two ways:

'https://api.twitter.com/1.1/statuses/update_with_media.json',
'https://upload.twitter.com/1/statuses/update_with_media.json',

version 1.1 and version 1 really dont know which the problem is, but is not possible upload an image for me :(

@oosswwaalldd

This comment has been minimized.

oosswwaalldd commented Sep 13, 2012

I think the problem is in the second parameter in the request done of the $connection object:

'https://api.twitter.com/1.1/statuses/update_with_media.json',

You must change it to:

'1.1/statuses/update_with_media.json',

Hope That works

@silvestret

This comment has been minimized.

silvestret commented Sep 18, 2012

no oosswaaaldd, i think we have to use the complete url to do the request to twiiter api not a relative.

@mwilber

This comment has been minimized.

mwilber commented Sep 26, 2012

I'm running into the same issue. v1 of the api (https://upload.twitter.com/1/statuses/update_with_media.json) but v1.1 (https://api.twitter.com/1.1/statuses/update_with_media.json) fails with code 0.

@mwilber

This comment has been minimized.

mwilber commented Sep 26, 2012

Disregard my last comment. The issue was unrelated to the upload url. The error came from a leading @ sign in the status message. In this case my script would tweet the photo to a specific user "@greenzeta here's your photo...". If I modify the message so the @ sign is not leading, "Here's your photo @greenzeta ..." it works. I haven't looked any more deeply than this so I don't know if the issue is with tmhOAuth or twitter itself.

@themattharris

This comment has been minimized.

Owner

themattharris commented Sep 26, 2012

@mwilber: the issue you had is discussed in issue #46.

@tharrosinfo

This comment has been minimized.

tharrosinfo commented Oct 17, 2012

I have been trying to get the update_with_media working for me but did not succeed. I just tried various formats with $image like with and without the {} with and without the @ sign, between " " and between ' ', I tried with status first and then media, and with media first then status, all gave the same response an error occurs updating the status.
I am running Apache with PHP 5.2 on WinXP. I tried 15, 16 and 17 october to run the code.

Just to be clear the following code I tried first and it works, just like it works with OAuth and Twitteroauth for php:

require_once($includepath.'tmhOAuth.php');
require_once($includepath.'tmhUtilities.php');
$connection = new tmhOAuth(array(
'consumer_key' => OAUTH_CONSUMER_KEY,
'consumer_secret' => OAUTH_CONSUMER_SECRET,
'user_token' => OAUTH_TOKEN_KEY,
'user_secret' => OAUTH_TOKEN_SECRET,
));

$code = $connection->request('POST', 'https://api.twitter.com/1.1/statuses/update.json',
array(
'status' => "'".$twitmsg."'"
),
true, // use auth
false // multipart
);

var_dump($connection->response);

Then I tried this code and get a Forbidden 403 response:

$image = 'recept001.jpg';

require_once($includepath.'tmhOAuth.php');
require_once($includepath.'tmhUtilities.php');
$connection = new tmhOAuth(array(
'consumer_key' => OAUTH_CONSUMER_KEY,
'consumer_secret' => OAUTH_CONSUMER_SECRET,
'user_token' => OAUTH_TOKEN_KEY,
'user_secret' => OAUTH_TOKEN_SECRET,
));

$code = $connection->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',   //_with_media
    array(
            'media[]'  => " @{$image}" ,  // note the space before the @ sign
        'status'   => ' upload image ' 
    ),
    true, // use auth
    true  // multipart
);

var_dump($connection->response);

Response (I removed codes and unnecessary entries):

array(7) {
["raw"]=>
string(1552) "HTTP/1.1 403 Forbidden
Date: Wed, 17 Oct 2012 05:24:15 GMT
Status: 403 Forbidden
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Set-Cookie: guest_id=; domain=.twitter.com; path=/; expires=Fri, 17-Oct-2014 17:24:15 GMT
Set-Cookie: dnt=; domain=.twitter.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
Set-Cookie: lang=en; path=/
Set-Cookie: lang=en; path=/
Set-Cookie: lang=en; path=/
Set-Cookie: lang=en; path=/
Set-Cookie: twid=(twid); domain=.twitter.com; path=/; secure
Set-Cookie: _twitter_sess= (session code) ; domain=.twitter.com; path=/; HttpOnly
X-Access-Level: read-write
Pragma: no-cache
X-Transaction-Mask: (transaction mask)
Content-Type: application/json; charset=utf-8
X-MID: (X_mid)
X-MediaRateLimit-Limit: 30
X-Runtime: 0.13306
X-Transaction: (X-transaction)
Expires: Tue, 31 Mar 1981 05:00:00 GMT
X-MediaRateLimit-Reset: 1350537855
X-MediaRateLimit-Class: photos
X-MediaRateLimit-Remaining: 30
X-Frame-Options: SAMEORIGIN
Last-Modified: Wed, 17 Oct 2012 05:24:15 GMT
Server: tfe
Content-Encoding: gzip
Content-Length: 82

{"errors":[{"code":189,"message":"Error creating status"}]}"
(...)
["request_header"]=>
string(619) "POST /1.1/statuses/update_with_media.json HTTP/1.1
User-Agent: tmhOAuth 0.7.0+SSL - //github.com/themattharris/tmhOAuth
Host: api.twitter.com
Accept: /
Accept-Encoding: deflate, gzip
Authorization: OAuth oauth_consumer_key="", oauth_nonce="", oauth_signature="", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1350451455", oauth_token="", oauth_version="1.0"
Content-Length: 274
Content-Type: multipart/form-data; boundary=----------------------------d63185680121

"
}
["error"]=>
string(0) ""
["errno"]=>
int(0)
}

@tharrosinfo

This comment has been minimized.

tharrosinfo commented Oct 17, 2012

I guess I may have resolved it.

Instead of referencing the filename in $image it should hold the binary data

so I added:

$filename = "recept001.jpg";
$handle = fopen($filename, "rb");
$image = fread($handle, filesize($filename));
fclose($handle);

then

'media[]' => "{$image};type=image/jpeg;filename={$filename}" ,

does the trick and the tweet is posted with an image.

@tsarma

This comment has been minimized.

tsarma commented Oct 18, 2012

In my case its still not working, even when I made the $image as binary data as mentioned above. I get response 0!!!
EDITED:
It works! my request url parameter was written wrong (../1/statuses/up.. instead of ../1.1/statuses/up.. )!! . And it works by the way without needing to fopen the image file. Just the right path to the image from the server root and it tweets.

@tharrosinfo

This comment has been minimized.

tharrosinfo commented Oct 18, 2012

add

var_dump($connection->response);

$connection stands for the variable declared with new tmhOAuth

then remove the personal info (tokens) and post the code and the result for review for others to help you

@themattharris

This comment has been minimized.

Owner

themattharris commented Oct 27, 2012

glad you got this fixed up and working.
and thanks to everyone else on this issue that helped along the way!

@formariz

This comment has been minimized.

formariz commented Dec 15, 2012

Guys, the exemple don't work for me.
Please, help me.

My code PHP

require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
  'consumer_key'    => '***************',
  'consumer_secret' => '***************',
  'user_token'      => '***************',
  'user_secret'     => '***************',
));

$image = 'sol.jpg';

$code = $tmhOAuth->request(
  'POST',
  'https://api.twitter.com/1.1/statuses/update_with_media.json',
  array(
    'media[]'  => "@{$image};type=image/jpeg;filename={$image}",
    'status'   => 'Picture time'
  ),
  true,
  true
);

var_dump($tmhOAuth->response);

if ($code == 200) {
  tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
  tmhUtilities::pr($tmhOAuth->response['response']);
}

And response

array(6) {
    ["raw"]=> string(0) ""
    ["code"]=> int(0)
    ["response"]=> bool(false)
    ["info"]=> array(22) {
        ["url"]=> string(62) "https://api.twitter.com/1.1/statuses/update_with_media.json"
        ["content_type"]=> NULL
        ["http_code"]=> int(0)
        ["header_size"]=> int(0)
        ["request_size"]=> int(0)
        ["filetime"]=> int(-1)
        ["ssl_verify_result"]=> int(0)
        ["redirect_count"]=> int(0)
        ["total_time"]=> float(0.936)
        ["namelookup_time"]=> float(0)
        ["connect_time"]=> float(0.281)
        ["pretransfer_time"]=> float(0.936)
        ["size_upload"]=> float(0)
        ["size_download"]=> float(0)
        ["speed_download"]=> float(0)
        ["speed_upload"]=> float(0)
        ["download_content_length"]=> float(-1)
        ["upload_content_length"]=> float(-1)
        ["starttransfer_time"]=> float(0)
        ["redirect_time"]=> float(0)
        ["certinfo"]=> array(0) { }
        ["redirect_url"]=> string(0) "" 
    }
    ["error"]=> string(29) "couldn't open file "sol.jpg" "
    ["errno"]=> int(26)
}

Any idea?

@tharrosinfo

This comment has been minimized.

tharrosinfo commented Dec 16, 2012

Is your $image the name of the image file or the binary data of the file? Because it needs to be the binary data, see above:

$filename = "recept001.jpg";
$handle = fopen($filename, "rb");
$image = fread($handle, filesize($filename));
fclose($handle);

@formariz

This comment has been minimized.

formariz commented Dec 17, 2012

i change my code:


$filename = "sol.jpg";
$handle = fopen($filename, "rb");
$image = fread($handle, filesize($filename));
fclose($handle);

$code = $tmhOAuth->request(
    'POST',
    'https://api.twitter.com/1.1/statuses/update_with_media.json',
    array(
        'media[]'  => "@{$image};type=image/jpeg;filename={$filename}",
        'status'   => 'Picture time',
      ),
      true, // use auth
      true  // multipart
);

Response:

array(6) {
    ["raw"]=> string(0) "" 
    ["code"]=> int(0) 
    ["response"]=> bool(false) 
    ["info"]=> array(22) { 
        ["url"]=> string(59) "https://api.twitter.com/1.1/statuses/update_with_media.json" 
        ["content_type"]=> NULL 
        ["http_code"]=> int(0) 
        ["header_size"]=> int(0) 
        ["request_size"]=> int(0) 
        ["filetime"]=> int(-1) 
        ["ssl_verify_result"]=> int(0) 
        ["redirect_count"]=> int(0) 
        ["total_time"]=> float(0.702) 
        ["namelookup_time"]=> float(0.156) 
        ["connect_time"]=> float(0.328) 
        ["pretransfer_time"]=> float(0.702) 
        ["size_upload"]=> float(0) 
        ["size_download"]=> float(0) 
        ["speed_download"]=> float(0) 
        ["speed_upload"]=> float(0) 
        ["download_content_length"]=> float(-1) 
        ["upload_content_length"]=> float(-1) 
        ["starttransfer_time"]=> float(0) 
        ["redirect_time"]=> float(0) 
        ["certinfo"]=> array(0) { } 
        ["redirect_url"]=> string(0) ""
    }
    ["error"]=> string(26) "couldn't open file "ÿØÿà" "
    ["errno"]=> int(26) 
}
@carlosacabrera

This comment has been minimized.

carlosacabrera commented Feb 13, 2013

@formariz try disabling SSL_VERIFYPEER

$connection = new tmhOAuth(array(
    'consumer_key'          => '(hidden)',
    'consumer_secret'       => '(hidden)',
    'user_token'            => '(hidden)',
    'user_secret'           => '(hidden)',
    'curl_ssl_verifypeer'   => false // <- This guy
));
@themattharris

This comment has been minimized.

Owner

themattharris commented Feb 19, 2013

@formariz you should be setting $image to the path to the file you wish to send as the photo - not the binary data contained within the file. here's an example:

let's say your file sol.jpg is in the same folder as your PHP code. in this example do this:

$image_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sol.jpg';
$name  = basename($image_path);

$code = $tmhOAuth->request(
    'POST',
    $tmhOAuth->url('1.1/statuses/update_with_media'),
    array(
        'media[]'  => "@{$image_path};type=image/jpeg;filename={$name}",
        'status'   => 'Picture time',
      ),
      true, // use auth
      true  // multipart
);

@carlosacabrera please don't change curl_ssl_verifypeer - for your peace of mind and that of your users it's very important the value stays as true - otherwise the SSL certificate for api.twitter.com won't be verified with the known root certificates.

@formariz

This comment has been minimized.

formariz commented Feb 20, 2013

Thank you @themattharris ... Worked perfectly for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment