Closed CanOzp closed 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'];
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...
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 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());`
@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.
@J7mbo I try it just now but I dont use PHPUnit_Framework_TestCase - am I wrong? I only use class TwitterAPIExchange
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.
@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.
$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. :-)
@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 ;)
Who can help me?
@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.
Ok I will do it, thx.
Hello I am trying to post tweet with media id but this code can post tweet without media. Could you help me?