From 98e8b88e7ae4adacbf2309738ebb70f4cb98729e Mon Sep 17 00:00:00 2001 From: Kengwang <1136772134@qq.com> Date: Wed, 1 Apr 2020 13:56:43 +0800 Subject: =?UTF-8?q?=F0=9F=94=A7=20Fixed:=20JSON=E5=8F=8B=E9=93=BE=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/Contents.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'libs/Contents.php') 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 '; $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,'
对不起, 您的主机不支持 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']; -- cgit v1.2.3