123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Services\Smm;
- use App\Services\Contracts\SmmPlatformInterface;
- use Abraham\TwitterOAuth\TwitterOAuth;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- class TwitterService implements SmmPlatformInterface
- {
- protected $configData;
- protected $twitterOAuth;
- protected $consumerKey;
- protected $consumerSecret;
- protected $oauthCallbackUrl;
- public function __construct($configData)
- {
- $this->configData = $configData;
- $this->consumerKey = env('TWITTER_CONSUMER_KEY');
- $this->consumerSecret = env('TWITTER_CONSUMER_SECRET');
- $this->oauthCallbackUrl = env('DIST_SITE_URL') . '/dist/callback/twitter';
- if (!empty($configData['access_token']) && !empty($configData['access_token_secret'])) {
- $this->twitterOAuth = new TwitterOAuth(
- $this->consumerKey,
- $this->consumerSecret,
- $configData['access_token'],
- $configData['access_token_secret']
- );
- }
- }
- public function login()
- {
- $connection = new TwitterOAuth($this->consumerKey, $this->consumerSecret);
- $requestToken = $connection->oauth('oauth/request_token', ['oauth_callback' => $this->oauthCallbackUrl]);
- session(['twitter_oauth_token' => $requestToken['oauth_token']]);
- session(['twitter_oauth_token_secret' => $requestToken['oauth_token_secret']]);
- $url = $connection->url('oauth/authorize', ['oauth_token' => $requestToken['oauth_token']]);
- return [
- 'status' => true,
- 'data' => ['url' => $url]
- ];
- }
- public function loginCallback(Request $request)
- {
- $oauthToken = $request->get('oauth_token');
- $oauthVerifier = $request->get('oauth_verifier');
- $requestToken = session('twitter_oauth_token');
- $requestTokenSecret = session('twitter_oauth_token_secret');
- $connection = new TwitterOAuth(
- $this->consumerKey,
- $this->consumerSecret,
- $requestToken,
- $requestTokenSecret
- );
- $accessToken = $connection->oauth('oauth/access_token', [
- 'oauth_verifier' => $oauthVerifier
- ]);
- return [
- 'status' => true,
- 'data' => [
- 'accessToken' => $accessToken['oauth_token'],
- 'accessTokenSecret' => $accessToken['oauth_token_secret'],
- 'userId' => $accessToken['user_id'],
- 'userName' => $accessToken['screen_name'],
- ]
- ];
- }
- public function postImage($message, $imagePaths, $accessToken = null)
- {
- try {
- $mediaIds = [];
- foreach ($imagePaths as $imagePath) {
- if (!file_exists($imagePath)) {
- throw new \Exception("图片不存在: {$imagePath}");
- }
- $uploadedMedia = $this->twitterOAuth->upload('media/upload', [
- 'media' => $imagePath
- ]);
- if (isset($uploadedMedia->error)) {
- throw new \Exception('媒体上传失败: ' . json_encode($uploadedMedia));
- }
- $mediaIds[] = $uploadedMedia->media_id_string;
- }
- $tweet = $this->twitterOAuth->post('statuses/update', [
- 'status' => $message,
- 'media_ids' => implode(',', $mediaIds)
- ]);
- if (isset($tweet->errors)) {
- throw new \Exception('推文发布失败: ' . json_encode($tweet->errors));
- }
- return [
- 'status' => true,
- 'data' => [
- 'tweetId' => $tweet->id_str,
- 'mediaIds' => $mediaIds
- ]
- ];
- } catch (\Exception $e) {
- return ['status' => false, 'data' => $e->getMessage()];
- }
- }
- public function postVideo($message, $videoPath, $accessToken = null)
- {
- try {
- if (!file_exists($videoPath)) {
- throw new \Exception("视频文件不存在: {$videoPath}");
- }
- $uploadedVideo = $this->twitterOAuth->upload('media/upload', [
- 'media' => $videoPath,
- 'media_type' => 'video/mp4'
- ], true); // 注意: chunked upload
- if (isset($uploadedVideo->error)) {
- throw new \Exception('视频上传失败: ' . json_encode($uploadedVideo));
- }
- $tweet = $this->twitterOAuth->post('statuses/update', [
- 'status' => $message,
- 'media_ids' => $uploadedVideo->media_id_string
- ]);
- if (isset($tweet->errors)) {
- throw new \Exception('推文发布失败: ' . json_encode($tweet->errors));
- }
- return [
- 'status' => true,
- 'data' => [
- 'tweetId' => $tweet->id_str,
- 'mediaId' => $uploadedVideo->media_id_string
- ]
- ];
- } catch (\Exception $e) {
- return ['status' => false, 'data' => $e->getMessage()];
- }
- }
- // 保持空实现
- public function getComments($postId) {}
- public function replyToComment($commentId) {}
- public function deleteComment($commentId) {}
- }
|