summaryrefslogtreecommitdiffstats
path: root/libs/Contents.php
diff options
context:
space:
mode:
authorEltrac Koalar <hi@guhub.cn>2020-04-01 14:10:13 +0800
committerGitHub <noreply@github.com>2020-04-01 14:10:13 +0800
commitbe512a764bdf9b68f1e93bb32414a5a6715c2a01 (patch)
tree5d66788861cea98c6f2515ee729748b433f4831c /libs/Contents.php
parentafa631ff8486d614a387759bdc8b473b1534404f (diff)
parent98e8b88e7ae4adacbf2309738ebb70f4cb98729e (diff)
Merge pull request #44 from kengwang/master
🔧 Fixed: JSON友链解析错误
Diffstat (limited to 'libs/Contents.php')
-rw-r--r--libs/Contents.php25
1 files changed, 20 insertions, 5 deletions
diff --git a/libs/Contents.php b/libs/Contents.php
index 0b1eaf7..b270059 100644
--- a/libs/Contents.php
+++ b/libs/Contents.php
@@ -85,13 +85,28 @@ class Contents
</a>
</div>';
$text = preg_replace($reg,$rp,$text);
-
//解析外部友链
- $reg = '/\[links data="(.*?)"\]/s';
- $dataLink = preg_replace($reg,'${1}',$text);
- $data = file_get_contents($dataLink);
- $data = json_decode($data, true);
+ $reg = '/\[links data="(.*?)"\]/';
+ $dataLink = preg_match($reg,$text,$matches);
+ if (!$dataLink) return $text; //普通文章别匹配!
+ $http=Typecho_Http_Client::get();
+ if (false == $http) {
+ $text=preg_replace($reg,'<br>对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使json友链功能',$text);
+ return $text;
+ }
+ try {
+ $result = $http->send($matches[1]);
+ }catch (Typecho_Http_Client_Exception $ex){
+ $text=preg_replace($reg, '对不起,json外链请求失败! 错误信息:'.$ex->getMessage(),$text);
+ return $text;
+ }
+ $data = json_decode($result, true);
+ if ($data==false) {//没获取到数据就别走了!赶紧返回免得搞出事情
+ $text=preg_replace($reg, '对不起,json外链解析失败!',$text);
+ return $text;
+ }
$linkItemNum = count($data);
+ $linksList='';//先Define一下,IDE在报错QAQ
for ($i = 0; $i < $linkItemNum; $i++) {
$name = $data[$i]['name'];
$link = $data[$i]['link'];