1. In channel product publishing, for the Weibo channel, when publishing an article of type: link (link), the Weibo platform responds with an error: status code: 400, error code: 20012, error message: Text too long, please input text less than 140 characters!, as shown in Figure 1

PS E:\wwwroot\channel-pub-api> ./yii pub-article-queue/run --verbose=1 --isolate=1 --color=0
2020-02-10 16:46:03 [pid: 550884] - Worker is started
2020-02-10 16:46:04 [81] common\jobs\PubArticleJob (attempt: 1, pid: 550884) - Started
2020-02-10 16:46:04 [81] common\jobs\PubArticleJob (attempt: 1, pid: 550884) - Error (0.547 s)
> yii\web\ServerErrorHttpException: Weibo's micro-connected web application interface HTTP request, third party sharing
a link to Weibo failed, status code: 400, error code: 20012, error message: Text too long, please input text less than
140 characters!
2020-02-10 16:46:04 [pid: 550884] - Worker is stopped (0:00:01)
2. After multiple tests, I determined the rule: the string length (with URLs already removed) must contain at most 130 characters. One Chinese character counts as one length, and one English letter/number counts as half a length. As shown in Figure 2

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章1内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
131(字符串长度,已经删除掉 URL) 182(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
3. Because the Weibo platform’s API documentation requires the Weibo content field value to include a URL, the channel publishing API synchronously requires the content field value to include both the content and the URL. As shown in Figure 3

4. Therefore, to implement synchronous validation in the channel publishing API, I need to remove the URL from the content field value and then calculate the string length. I searched Google for: php delete url in string, and found the search result PHP – Remove URLs from string at https://gist.github.com/madeinnordeste/e071857148084da94891 , as shown in Figure 4

5. In the model’s validation rule method, I remove the URL from the article content to get the string length. One Chinese character counts as two lengths, so the article content can contain at most 130 characters (checking if the length is greater than 260). To get the string length, you can refer to: https://www.shuijingwanwq.com/2015/11/03/725/
/**
* Validates the content.
* This method serves as the inline validation for content.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validateContent($attribute, $params)
{
if (!$this->hasErrors()) {
// 检查安全域名列表中的域名是否存在于文章内容中
$safeDomainNames = explode(",", Yii::$app->params['weiboAuth']['weiboConnectWebApp']['safeDomainName']);
$isExist = false; // 是否存在
foreach ($safeDomainNames as $safeDomainName) {
$httpPos = stripos($this->$attribute, 'http://' . $safeDomainName);
$httpsPos = stripos($this->$attribute, 'https://' . $safeDomainName);
// 判断安全域名是否存在,如果存在,则退出循环
if ($httpPos !== false || $httpsPos !== false) {
$isExist = true;
break;
}
}
if (!$isExist) {
$this->addError($attribute, Yii::t('error', '244009'));
}
// 从文章内容中删除 URL
$content = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $this->$attribute);
// 获取字符串长度,一个汉字表示两个长度
$length = StringHelper::strlenTwo($content);
// 文章内容只能包含至多130个字符
if ($length > 260) {
$this->addError($attribute, Yii::t('error', '244010'));
}
}
}
6. I printed the values ($content, $length) for the 4 examples from step 2 in turn and called the API; the results matched expectations, as shown in Figure 5

文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章
$length:260
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章1内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
131(字符串长度,已经删除掉 URL) 182(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章1内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章
$length:261
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111内容文章内容文章内容文章内容文章
$length:260
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
10000(发布成功)
134(字符串长度,已经删除掉 URL) 185(字符串长度,保留 URL) 130(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
$length:260
7. I discovered a bug: when a letter/number appears immediately before the URL, printing the values ($content, $length) for the 2 examples in turn shows that the URL is not removed, which does not match expectations
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 129(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
$length:309
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aahttps://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
202153(发布失败)
136(字符串长度,已经删除掉 URL) 187(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aahttps://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
$length:313
8. Reference URL: https://stackoverflow.com/questions/24588470/php-remove-url-from-string . After comparative analysis, I decided to remove: \b. I printed the values ($content, $length) for the 2 examples, and the URL was successfully removed, matching expectations
$content = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $this->$attribute);
$regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?).*$)@";
echo preg_replace($regex, ' ', $string);
$content = preg_replace('/(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $this->$attribute); // 删除:\b
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00https://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html
202153(发布失败)
130(字符串长度,已经删除掉 URL) 181(字符串长度,保留 URL) 129(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容00
$length:258
文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aahttps://wjdev.chinamcloud.cn/www/wwwwss/wwwwww.html内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
202153(发布失败)
136(字符串长度,已经删除掉 URL) 187(字符串长度,保留 URL) 131(weibo,字符串长度,已经删除掉 URL)
$content:文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章aa内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章11111111
$length:262
