|
@@ -324,6 +324,9 @@ class TwitterService implements SmmPlatformInterface
|
|
|
*/
|
|
|
public function uploadAndPost($filePaths, $tweetText) {
|
|
|
try {
|
|
|
+
|
|
|
+ $tweetText = $this->truncateText($tweetText, 138);// 截断推文
|
|
|
+
|
|
|
$media_ids = [];
|
|
|
foreach ($filePaths as $filePath) {
|
|
|
$file_info = pathinfo($filePath);
|
|
@@ -450,4 +453,30 @@ class TwitterService implements SmmPlatformInterface
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 安全截断多字节文本(支持中文、日文等)
|
|
|
+ * @param string $text 原始文本
|
|
|
+ * @param int $maxLength 最大长度(默认280)
|
|
|
+ * @param string $ellipsis 截断后缀(默认...)
|
|
|
+ * @return string 处理后的文本
|
|
|
+ */
|
|
|
+ function truncateText(string $text, int $maxLength = 140, string $ellipsis = ''): string {
|
|
|
+ // 检测文本长度
|
|
|
+ $textLength = mb_strlen($text, 'UTF-8');
|
|
|
+
|
|
|
+ if ($textLength <= $maxLength) {
|
|
|
+ return $text;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算后缀长度
|
|
|
+ $ellipsisLength = mb_strlen($ellipsis, 'UTF-8');
|
|
|
+
|
|
|
+ // 确保后缀不会导致总长度超过限制
|
|
|
+ $truncateLength = $maxLength - $ellipsisLength;
|
|
|
+
|
|
|
+ // 安全截取并添加后缀
|
|
|
+ return mb_substr($text, 0, $truncateLength, 'UTF-8') . $ellipsis;
|
|
|
+ }
|
|
|
}
|