social-webcollage/twitter.php

40 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2021-02-26 13:17:32 +01:00
<?php
use Abraham\TwitterOAuth\TwitterOAuth;
use Abraham\TwitterOAuth\TwitterOAuthException;
use Shikiryu\WebGobbler\Assembler\Superpose;
use Shikiryu\WebGobbler\Config;
use Shikiryu\WebGobbler\Pool;
include 'vendor/autoload.php';
$twitter_config = require 'twitter-config.php';
2021-02-26 13:17:32 +01:00
try {
// generate picture
$config = new Config('./config.json');
$pool = new Pool($config);
$upload_image_name = uniqid('superpose_', true);
$image = new Superpose($pool, $config);
$image->saveTo(sprintf('./render/%s.jpg', $upload_image_name));
// send picture to twitter
$connection = new TwitterOAuth(
$twitter_config['api_key'],
$twitter_config['api_secret'],
$twitter_config['access_token'],
$twitter_config['access_secret']
);
$connection->setTimeouts(5, 60000);
$collage = $connection->upload('media/upload', ['media' => realpath(sprintf('./render/%s.jpg', $upload_image_name))]);
$parameters = [
'status' => 'Nouveau collage',
'media_ids' => $collage->media_id_string,
];
$result = $connection->post('statuses/update', $parameters);
} catch (TwitterOAuthException | Exception $exception) {
echo $exception->getMessage();
}