TwitterService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Services\Smm;
  3. use App\Services\Contracts\SmmPlatformInterface;
  4. use Abraham\TwitterOAuth\TwitterOAuth;
  5. use Carbon\Carbon;
  6. use CURLFile;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Log;
  9. class TwitterService implements SmmPlatformInterface
  10. {
  11. protected $configData;
  12. protected $twitterOAuth;
  13. protected $consumerKey;
  14. protected $consumerSecret;
  15. protected $oauthCallbackUrl;
  16. public function __construct($configData)
  17. {
  18. $this->configData = $configData;
  19. $this->consumerKey = env('SSM_X_CONSUMER_KEY');
  20. $this->consumerSecret = env('SSM_X_CONSUMER_SECRET');
  21. $this->oauthCallbackUrl = env('DIST_SITE_URL') . '/dist/callback/twitter';
  22. if (isset($configData['accountInfo'])) {
  23. //初始化1.1版本的配置信息
  24. $access_token = $configData['accountInfo']['access_token'];
  25. $backup_field1 = json_decode($configData['accountInfo']['backup_field1'],true);
  26. $access_token_secret = $backup_field1['accessTokenSecret'];
  27. if (!empty($access_token) && !empty($access_token_secret)) {
  28. $this->twitterOAuth = new TwitterOAuth(
  29. $this->consumerKey,
  30. $this->consumerSecret,
  31. $access_token,
  32. $access_token_secret
  33. );
  34. }
  35. }
  36. }
  37. public function login()
  38. {
  39. $connection = new TwitterOAuth($this->consumerKey, $this->consumerSecret);
  40. $requestToken = $connection->oauth('oauth/request_token', ['oauth_callback' => $this->oauthCallbackUrl]);
  41. session(['twitter_oauth_token' => $requestToken['oauth_token']]);
  42. session(['twitter_oauth_token_secret' => $requestToken['oauth_token_secret']]);
  43. $url = $connection->url('oauth/authorize', ['oauth_token' => $requestToken['oauth_token']]);
  44. return [
  45. 'status' => true,
  46. 'data' => ['url' => $url]
  47. ];
  48. }
  49. public function loginCallback(Request $request)
  50. {
  51. $oauthToken = $request->get('oauth_token');
  52. $oauthVerifier = $request->get('oauth_verifier');
  53. $requestToken = session('twitter_oauth_token');
  54. $requestTokenSecret = session('twitter_oauth_token_secret');
  55. $connection = new TwitterOAuth(
  56. $this->consumerKey,
  57. $this->consumerSecret,
  58. $requestToken,
  59. $requestTokenSecret
  60. );
  61. $accessToken = $connection->oauth('oauth/access_token', [
  62. 'oauth_verifier' => $oauthVerifier
  63. ]);
  64. $expiresAt = Carbon::now()->addDays(90)->format('Y-m-d H:i:s');
  65. return [
  66. 'status' => true,
  67. 'data' => [
  68. 'accessToken' => $accessToken['oauth_token'],
  69. 'backupField1' => json_encode(['accessTokenSecret'=>$accessToken['oauth_token_secret']]),
  70. 'userId' => $accessToken['user_id'],
  71. 'userName' => $accessToken['screen_name'],
  72. 'accessToken_expiresAt' => $expiresAt,
  73. ]
  74. ];
  75. }
  76. public function postImage($message, $imagePaths, $accessToken = null)
  77. {
  78. Log::info('开始发布图片帖子');
  79. try {
  80. $this->twitterOAuth->setApiVersion('1.1'); // 强制使用 v1.1 API
  81. $this->twitterOAuth->setTimeouts(10, 30);
  82. $mediaIds = [];
  83. foreach ($imagePaths as $imagePath) {
  84. $imagePath = toStoragePath($imagePath);
  85. if (!file_exists($imagePath)) {
  86. throw new \Exception("图片不存在: {$imagePath}");
  87. }
  88. //1.1版本上传媒体文件
  89. $uploadedMedia = $this->twitterOAuth->upload('media/upload', [
  90. 'media' => $imagePath
  91. ]);
  92. Log::info('1.1版本上传媒体文件完成');
  93. if (isset($uploadedMedia->error)) {
  94. throw new \Exception('媒体上传失败: ' . json_encode($uploadedMedia));
  95. }
  96. $mediaIds[] = $uploadedMedia->media_id_string;
  97. }
  98. // 2.0版本发布推文
  99. $this->twitterOAuth->setApiVersion('2');
  100. $tweet = $this->twitterOAuth->post('tweets', [
  101. 'text' => $message,
  102. 'media' => !empty($mediaIds) ? ['media_ids' => $mediaIds] : null
  103. ]);
  104. Log::info('2.0版本发布推文完成');
  105. if (isset($tweet->errors)) {
  106. throw new \Exception('推文发布失败: ' . json_encode($tweet->errors));
  107. }
  108. return ['status' => true,
  109. 'data' => [
  110. 'responseIds' => [$tweet->data->id],
  111. ]];
  112. } catch (\Exception $e) {
  113. return ['status' => false, 'data' => $e->getMessage()];
  114. }
  115. }
  116. public function postVideo($message, $videoPath, $accessToken = null)
  117. {
  118. try {
  119. $this->twitterOAuth->setTimeouts(10, 120);
  120. $this->twitterOAuth->setApiVersion('1.1');
  121. $videoPath = toStoragePath($videoPath);
  122. // Verify file exists and is readable
  123. if (!file_exists($videoPath) || !is_readable($videoPath)) {
  124. throw new Exception("Video file does not exist or is not readable: $videoPath");
  125. }
  126. // Upload video with chunked upload
  127. $uploadedMedia = $this->twitterOAuth->upload('media/upload', [
  128. 'media' => $videoPath,
  129. 'media_category' => 'tweet_video'
  130. ],['chunkedUpload'=>true]);
  131. Log::info('1.1版本,分块上传视频');
  132. $code = $this->twitterOAuth->getLastHttpCode();
  133. if (isset($uploadedMedia->error) || $code != 200) {
  134. throw new \Exception('媒体上传失败: ' . json_encode($uploadedMedia));
  135. }
  136. $mediaId = $uploadedMedia->media_id_string;
  137. $limit = 0;
  138. do {
  139. $status = $this->twitterOAuth->mediaStatus($mediaId);
  140. Log::info('检测视频上传状态'.print_r($status,true));
  141. sleep(5); // 等待 5 秒
  142. $limit++;
  143. } while ($status->processing_info->state !== 'succeeded' && $limit <= 3); // 最多重试 3 次
  144. if ($status->processing_info->state === 'succeeded') {
  145. //print_r($status->processing_info->state);
  146. // 2.0版本发布推文
  147. $this->twitterOAuth->setApiVersion('2');
  148. $tweet = $this->twitterOAuth->post('tweets', [
  149. 'text' => $message,
  150. 'media' => ['media_ids' => [$mediaId]]
  151. ]);
  152. Log::info('2.0版本发布推文');
  153. if (isset($tweet->errors)) {
  154. throw new \Exception('推文发布失败: ' . json_encode($tweet->errors));
  155. }
  156. return ['status' => true,
  157. 'data' => [
  158. 'responseIds' => [$tweet->data->id],
  159. ]
  160. ];
  161. } else {
  162. return ['status' => false, 'data' => '视频上传失败或处理超时。' ];
  163. }
  164. } catch (\Exception $e) {
  165. return ['status' => false, 'data' => $e->getMessage()];
  166. }
  167. }
  168. // 保持空实现
  169. public function getComments($postId) {}
  170. public function replyToComment($commentId) {}
  171. public function deleteComment($commentId) {}
  172. }