應大家需求,出一個 wp 自動發佈每日 60 秒讀懂世界文章的教程. 1.複製下方的 php 代碼 <?php $date = file_get_contents("https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items"); ...
應大家需求,出一個 wp 自動發佈每日 60 秒讀懂世界文章的教程.
1.複製下方的 php 代碼
<?php $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; // 創建文件夾保存圖片 if (!file_exists("60s")){mkdir ("60s",0777,true); } imagejpeg($new_image,"60s/".$rand_name); imagedestroy($src); imagedestroy($new_image); $content = strip_tags($content,'<p>'); $content = '<img class="size-full wp-image-156 aligncenter"src=" 你的網站 /60s/'.$rand_name.'" alt=""width="720"height="350"/>'.$content; require __DIR__ . '/wp-config.php'; global $wpdb; date_default_timezone_set('PRC'); $post_tag_arr = array(); // 先檢查文章分類是否存在 $term_taxonomy_id = $wpdb->get_row("SELECT tt.term_taxonomy_id from $wpdb->terms t join $wpdb->term_taxonomy tt on t.term_id = tt.term_id where t.name =' 每天 60 秒讀懂世界 'and tt.taxonomy ='category' ")->term_taxonomy_id; if (!$term_taxonomy_id) {$wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES(' 每天 60 秒讀懂世界 ','60miao','0')"); $category_id = $wpdb->insert_id; $wpdb->query("insert into $wpdb->term_taxonomy (term_id,taxonomy,description,parent,count)VALUES($category_id,'category','','0','1')"); $term_taxonomy_id = $wpdb->insert_id; } $post_tag_arr[] = $term_taxonomy_id; $html = $content; // 標題 $title = $date->data[0]->title; // 標題存在則不插入 $posts = $wpdb->get_row("SELECT id from $wpdb->posts where post_title ='$title' "); if (!$posts) {$now = current_time('mysql'); $now_gmt = current_time('mysql', 1); $wpdb->insert( $wpdb->posts, array( 'post_author' => 1, 'post_date' => $now, 'post_date_gmt' => $now_gmt, 'post_content' => $html, 'post_title' => $title, 'post_excerpt' => '','post_status'=>'publish','comment_status'=>'open','ping_status'=>'open','post_password'=>'', 'post_name' => $title, 'to_ping' => '','pinged'=>'', 'post_modified' => $now, 'post_modified_gmt' => $now_gmt, 'post_content_filtered' => '','post_parent'=>'0','guid'=>'',// 文章鏈接 插入後修改 'menu_order' => '0', 'post_type' => 'post', 'post_mime_type' => '','comment_count'=>'0', ) ); $insertid = $wpdb->insert_id; $post_guid = get_option('home') . '/?p=' . $insertid; $wpdb->query("UPDATE $wpdb->posts SET guid=$post_guid where id = $insertid"); // 插入文章和分類、標簽、專題的關係 $sql = "INSERT INTO $wpdb->term_relationships (object_id,term_taxonomy_id,term_order) VALUES"; foreach ($post_tag_arr as $key => $value) {$sql .= "($insertid, $value,'0'),"; } $wpdb->query(rtrim($sql, ",")); }
2. 去到網站的根目錄新建 PHP 文件,粘貼剛剛複製的代碼文件名命名 60s
3. 修改剛剛代碼中第 33 行的地址,改成自己站點的地址
4. 在寶塔後臺設置定時訪問的任務:你網站 /60s.php