TwitterService.php 7.2 KB

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