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

Post Status with media #246

Closed
CanOzp opened this issue Sep 8, 2017 · 14 comments
Closed

Post Status with media #246

CanOzp opened this issue Sep 8, 2017 · 14 comments

Comments

@CanOzp
Copy link

CanOzp commented Sep 8, 2017

Hello
I am trying to post tweet with media id but this code can post tweet without media. Could you help me?

require_once('TwitterAPIExchange.php');
$settings = array(
    'oauth_access_token' => "******",
    'oauth_access_token_secret' => "******",
    'consumer_key' => "******",
    'consumer_secret' => "******"
);
$twitter = new TwitterAPIExchange($settings);
    				$url = "https://api.twitter.com/1.1/statuses/update.json";
				$twitter = new TwitterAPIExchange($settings);
				$requestMethod = 'POST';
				$response = $twitter->setPostfields(
					array('status' => 'TEST Tweet', 'possibly_sensitive' => false, 'media_ids' => 906125425373360128)
				)
									->buildOauth($url, $requestMethod)
									->performRequest();
@J7mbo
Copy link
Owner

J7mbo commented Sep 8, 2017

This test shows uploading a media to get an id.

This test shows tweeting with the previously retrieved media id.

The tests pass so the code works! :)

@CanOzp
Copy link
Author

CanOzp commented Sep 8, 2017

Thank you for respond. I copied uploading media code and now i get this error "Fatal error: Using $this when not in object context in /home/..."

error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);

$twitter = new TwitterAPIExchange($settings);
        $file = file_get_contents(__DIR__ . '/doviz.jpg');
        $data = base64_encode($file);
        $url    = 'https://upload.twitter.com/1.1/media/upload.json';
        $method = 'POST';
        $params = array(
            'media_data' => $data
        );
        $data     = $this->exchange->request($url, $method, $params);
        $expected = 'image\/png';
        $this->assertContains($expected, $data);
        /** Store the media id for later **/
        $data = @json_decode($data, true);
        $this->assertArrayHasKey('media_id', is_array($data) ? $data : array());
        self::$mediaId = $data['media_id'];

@J7mbo
Copy link
Owner

J7mbo commented Sep 8, 2017

I'd recommend googling errors like "Fatal error: Using $this when not in object context". It means you haven't put them in an object, so $this doesn't refer to anything. You can't just copy and paste code and expect it to work...

@J7mbo
Copy link
Owner

J7mbo commented Sep 21, 2017

Closing this as no response and it doesn't seem directly related to the library but more to PHP instead. If this is not the case, please feel free to re-open / create a new issue.

@J7mbo J7mbo closed this as completed Sep 21, 2017
@koese66
Copy link

koese66 commented Feb 8, 2019

@J7mbo I need help please. I get the same error.
Here ist my code.
// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$requestMethod = "POST";
$media = file_get_contents($temp);
$params = array(
"media" => $media,
);

                $twitter = new TwitterAPIExchange($settings);

                $res = $twitter->setPostFields($params)
                    ->buildOauth($url, $requestMethod)
                    ->performRequest();

                // Result is a json string
                $result = json_decode($res);

                $media_ids = $result->media_id_string;

                // Post on Twitter
                $url = "https://api.twitter.com/1.1/statuses/update.json";
                $requestMethod = "POST";

                $postfields = array(
                    'status' => $status,
                    'media_ids' => $media_ids);

                $twitter = new TwitterAPIExchange($settings);
                $resultat = json_decode($twitter->buildOauth($url, $requestMethod)
                    ->setPostfields($postfields)
                    ->performRequest());` 

@J7mbo
Copy link
Owner

J7mbo commented Feb 8, 2019

@koese66 Can you please print the error that you get?

Also, can you verify that you have looked at the tests from here: #246 (comment), ensured you are doing the same thing? Because the tests pass and they prove that what you are trying to do works.

@koese66
Copy link

koese66 commented Feb 8, 2019

@J7mbo I try it just now but I dont use PHPUnit_Framework_TestCase - am I wrong?
I only use class TwitterAPIExchange

@J7mbo
Copy link
Owner

J7mbo commented Feb 8, 2019

You don't need PHPUnit_Framework_TestCase, that's there because this is an automated test. So you don't need: $this->assertContains($expected, $data); or $this->assertArrayHasKey('media_id', is_array($data) ? $data : array());.

Instead of self::$mediaId = $data['media_id'];, just say $mediaId = $data['media_id'] and then you should have the value you need in $mediaId to make the second request which you can see in the second test.

@koese66
Copy link

koese66 commented Feb 8, 2019

@J7mbo Ok so far what is with this:
$data = $this->exchange->request($url, $method, $params);
Is this ok? Do I have change it also?
Because my script doesnt work yet.

@J7mbo
Copy link
Owner

J7mbo commented Feb 8, 2019

$this refers to the current object. So you just want $exchange, or whatever your twitterapiexchange object is stored in.

If you need help with writing PHP in general, please post a question on StackOverflow as this is just for library problems. :-)

@koese66
Copy link

koese66 commented Feb 8, 2019

@J7mbo Hey where can I post my own code? My script posted a status but without the image. I need help :) thx for your help ;)

@koese66
Copy link

koese66 commented Feb 8, 2019

Who can help me?

@J7mbo
Copy link
Owner

J7mbo commented Feb 8, 2019

@koese66 If you open a new ticket I'll do my best - the original issue author is likely to be receiving emails because you are commenting on his ticket.

@koese66
Copy link

koese66 commented Feb 8, 2019

Ok I will do it, thx.

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

3 participants