J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 802 forks source link

Post Status with media #246

Closed CanOzp closed 6 years ago

CanOzp commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.

koese66 commented 5 years ago

@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 commented 5 years ago

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

Also, can you verify that you have looked at the tests from here: https://github.com/J7mbo/twitter-api-php/issues/246#issuecomment-328087306, ensured you are doing the same thing? Because the tests pass and they prove that what you are trying to do works.

koese66 commented 5 years ago

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

J7mbo commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

$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 commented 5 years ago

@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 commented 5 years ago

Who can help me?

J7mbo commented 5 years ago

@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 commented 5 years ago

Ok I will do it, thx.