最近看各個站點都有的每日六十秒讀世界的文章或者頁面,大多數實現方式大致兩種:一種是emlog中相關的插件或者采集,此方法雖然配置簡單、方便,但是這個采集文章內容具有很強的時效性,需要每日定時訪問觸發采集才能發布文章;
一種是直接引用別人的六十秒讀世界的api,此方法最大的優點是不用設置采集和發布大量文章,但是形式是僅僅的一張圖片,這就過于簡單的了,那有還有比前兩種更好點又不太簡單的實現方式?,答案有是有的,其實就是將接口寫在主題中。
實現方式
1.在模板主題的module.php文件添加以下函數
<?php //獨立頁面-每日60秒 function luyu_sixtys(){ $date = file_get_contents("https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items"); $date = json_decode($date); $content = $date->data[0]->content; $content = preg_replace('/(<a.*?>[\s\S]*?<\/a>)/','',$content); $pattern ='<img.*?src="(.*?)">'; preg_match($pattern,$content,$matches); $src_path = $matches[1]; $src = imagecreatefromstring(file_get_contents($src_path)); $info = getimagesize($src_path); $x = 0; $y = 0; $width = 720; $height = 350; $final_width = 720; $final_height = round($final_width * $height / $width); $new_image = imagecreatetruecolor($final_width, $final_height); imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height); $ext = pathinfo($src_path, PATHINFO_EXTENSION); $rand_name = date("Ymd") . "." . $ext; $url= BLOG_URL; if (!file_exists("content/uploadfile/60s")){ mkdir ("content/uploadfile/60s",0777,true); } imagejpeg($new_image,"content/uploadfile/60s/".$rand_name); imagedestroy($src); imagedestroy($new_image); $content = strip_tags($content,'<p>'); $content = '<img class="" src="'.$url.'/content/uploadfile/60s/'.$rand_name.'" />'.$content; return $content; }; ?>
2.在自己使用的模板創建獨立頁面,以60s.php為例,根據各自的主題在文章內容部分添加調用顯示。
<?php echo luyu_sixtys()?>
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
相關文章
暫無評論...