Skip to content
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

Posting image with tweet #181

Closed
cbielich opened this issue Mar 7, 2016 · 17 comments
Closed

Posting image with tweet #181

cbielich opened this issue Mar 7, 2016 · 17 comments

Comments

@cbielich
Copy link

cbielich commented Mar 7, 2016

Maybe I am missing something but is there a way to post an image with a tweet with your code?

As an example code I am trying something like this

require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
$url = "https://api.twitter.com/1.1/statuses/update.json";

$requestMethod = 'POST';

$twimage = 'image.jpg';

$postfields = array(
'media[]' => "@{$twimage}",
'status' => 'Testing Twitter app'
);

$twitter = new TwitterAPIExchange($settings);

$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();

print_r($response);

@BugHunter2k
Copy link

BugHunter2k commented Mar 7, 2016

You have to upload the image first. The api gives you an ID wich you send as media with yout post. eg:

require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
$twitter = new TwitterAPIExchange($settings);

// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$method = "POST";
$params = array(
    "media_data" => $image,
);


$json = $twitter
            ->setPostfields($params)
            ->buildOauth($url, $method)
            ->performRequest();

// Result is a json string
$res = json_decode($json);
// Extract media id
$id = $res->media_id_string;

// Send tweet with uploaded image
$params= array('media_ids' => $id);

$json = $twitter
            ->setPostfields($params)
            ->buildOauth($url, $method)
            ->performRequest();

@cbielich
Copy link
Author

cbielich commented Mar 7, 2016

What is $this? and where does my $settings go so I cant send my tokens and such.

@BugHunter2k
Copy link

Oh, sorry
Just copied some line from my class. $this is the twitter class. You can use $twitter in your case.
Your settings will go in the initialization like you have done this far.

@helloarpitgshah
Copy link

Hey,

I got the response with the media_id_string but whenever I can check with the twitter, I can't find the images.

Can you please let me know about the issues?

Thanks,
Arpit G Shah

@gnanasekaranl
Copy link

HI ,
I can't tweet with image. I use above code i get API not defined error

ex: $json = $this->api

@BugHunter2k
Copy link

BugHunter2k commented May 25, 2016

Code above was just a little copy paste out of a bigger project. Made a little modification with api-init. Haven't tested the changes, but give it a try.

@gnanasekaranl
Copy link

@BugHunter2k Not working. give me any other option

@J7mbo
Copy link
Owner

J7mbo commented May 26, 2016

Take a look at the tests, they're usually a great indicator of how to do things.

Firstly, upload the image. Here's testMediaUpload().

Then, the following test creates a tweet with the id of the uploaded image.

Give that a try, they work otherwise the tests would be failing for everyone.

@gnanasekaranl
Copy link

Thanks for reply.
This test function, how can I run the function. Give me a example

@gnanasekaranl
Copy link

Finally I did.

$file = file_get_contents(DIR . '/img/logo.png');
$data = base64_encode($file);

// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$method = "POST";
$params = array(
"media_data" => $data,
);

$json = $twitter
->buildOauth($url, $method)
->setPostfields($params)
->performRequest();

// Result is a json string
$res = json_decode($json);
// Extract media id
$id = $res->media_id_string;

$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'media_ids' => $id,
'status' => 'This is machine test' );

if(strlen($postfields['status']) <= 140)
{
//$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}else{
echo "140 char exceed";
}

@J7mbo
Copy link
Owner

J7mbo commented Jul 25, 2016

Hi, this is an old one. I'm just going through and cleaning up issues, although if you still need help, please feel free to reopen a new issue. Thanks.

@J7mbo J7mbo closed this as completed Jul 25, 2016
@cbielich
Copy link
Author

cbielich commented Feb 8, 2017

I cant even remember what I did to fix it lol

@webboyon
Copy link

Hi all this what got me
twiiter says
media required // really!!!
media_data required

all you have to do is create the array and only use media_data => base64_encode(file_get_contents(...));
Working prefect for uploding image and video

@birdbreath
Copy link

Returns:

Fatal error: Uncaught Error: Call to a member function buildOauth() on null

Any
Screen Shot 2020-10-05 at 5 34 01 PM copy
idea why?

@birdbreath
Copy link

line 32

@stepinnsolution2
Copy link

Did above code work?

@birdbreath
Copy link

No, but this revised one did:
php2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants