WordPress自动发表每日60秒读懂世界文章方法

2022年12月1日17:34:18 1
广告也精彩

WordPress自动发表每日60秒读懂世界文章方法

一、每日60秒读懂世界文章版本,内容由知乎https://www.zhihu.com提供,是将60秒读懂世界内容写入网站数据库。

使用方法

1、下载源文件(下载地址在文章底部)

2、放在网站根目录下

3、将32行链接修改为个人网站链接

4、宝塔设置计划任务,访问url,输入你的域名/60s.php

1、PHP代码:

  1. <?php
  2. $date = file_get_contents("https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items");
  3. $date = json_decode($date);
  4. $content = $date->data[0]->content;
  5. $pattern ='<img.*?src="(.*?)">';
  6. preg_match($pattern,$content,$matches);
  7. $src_path = $matches[1];
  8. $src = imagecreatefromstring(file_get_contents($src_path));
  9. $info = getimagesize($src_path);
  10. //裁剪开区域左上角的点的坐标
  11. $x = 0;
  12. $y = 0;
  13. //裁剪区域的宽和高
  14. $width = 720;
  15. $height = 350;
  16. //最终保存成图片的宽和高,和源要等比例,否则会变形
  17. $final_width = 720;
  18. $final_height = round($final_width * $height / $width);
  19. //将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
  20. $new_image = imagecreatetruecolor($final_width$final_height);
  21. imagecopyresampled($new_image$src, 0, 0, $x$y$final_width$final_height$width$height);
  22. $ext = pathinfo($src_path, PATHINFO_EXTENSION);
  23. $rand_name = date("Ymd") . "." . $ext;
  24. //创建文件夹保存图片
  25. if (!file_exists("60miao")){
  26.     mkdir ("60s",0777,true);
  27. }
  28. imagejpeg($new_image,"60s/".$rand_name);
  29. imagedestroy($src);
  30. imagedestroy($new_image);
  31. $content = strip_tags($content,'<p>');
  32. $content = '<img class="size-full wp-image-156 aligncenter" src="https://www.tsxxc.com/60s/'.$rand_name.'"  width="720" height="350" />'.$content;
  33. require __DIR__ . '/wp-config.php';
  34. global $wpdb;
  35. date_default_timezone_set('PRC');
  36. $post_tag_arr = array();
  37. //先检查文章分类是否存在
  38. $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;
  39. if (!$term_taxonomy_id) {
  40.     $wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES('每天60秒读懂世界','60秒','0')");
  41.     $category_id = $wpdb->insert_id;
  42.     $wpdb->query("insert into $wpdb->term_taxonomy (term_id,taxonomy,description,parent,count)VALUES($category_id,'category','','0','1')");
  43.     $term_taxonomy_id = $wpdb->insert_id;
  44. }
  45. $post_tag_arr[] = $term_taxonomy_id;
  46. $html = $content;
  47. //标题
  48. $title = $date->data[0]->title;
  49. //标题存在则不插入
  50. $posts = $wpdb->get_row("SELECT id from $wpdb->posts where post_title = '$title' ");
  51. if (!$posts) {
  52.     $now = current_time('mysql');
  53.     $now_gmt = current_time('mysql', 1);
  54.     $wpdb->insert(
  55.         $wpdb->posts,
  56.         array(
  57.             'post_author' => 1,
  58.             'post_date' => $now,
  59.             'post_date_gmt' => $now_gmt,
  60.             'post_content' => $html,
  61.             'post_title' => $title,
  62.             'post_excerpt' => '',
  63.             'post_status' => 'publish',
  64.             'comment_status' => 'open',
  65.             'ping_status' => 'open',
  66.             'post_password' => '',
  67.             'post_name' => $title,
  68.             'to_ping' => '',
  69.             'pinged' => '',
  70.             'post_modified' => $now,
  71.             'post_modified_gmt' => $now_gmt,
  72.             'post_content_filtered' => '',
  73.             'post_parent' => '0',
  74.             'guid' => '',//文章链接 插入后修改
  75.             'menu_order' => '0',
  76.             'post_type' => 'post',
  77.             'post_mime_type' => '',
  78.             'comment_count' => '0',
  79.         )
  80.     );
  81.     $insertid = $wpdb->insert_id;
  82.     $post_guid = get_option('home') . '/?p=' . $insertid;
  83.     $wpdb->query(" UPDATE $wpdb->posts SET guid=$post_guid where id = $insertid ");
  84.     //插入文章和分类、标签、专题的关系
  85.     $sql = " INSERT INTO $wpdb->term_relationships (object_id,term_taxonomy_id,term_order) VALUES ";
  86.     foreach ($post_tag_arr as $key => $value) {
  87.         $sql .= "($insertid, $value, '0'),";
  88.     }
  89.     $wpdb->query(rtrim($sql","));
  90. }

下载地址:

https://cjw41415.lanzouf.com/b01w3c9xg

提取码:

当前部分内容需要密码查看:

1、扫码关注公众号回复: 密码

2、根据提示回复: 2111 即可获取文章阅读密码

 

  • 我的微信
  • 有问题可随时与我联系
  • weinxin
  • 微信小程序
  • 随时随地查看文章
  • weinxin
ts小陈

发表评论(请规范评论)--评论需审核

:?: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :cry: :mrgreen: :neutral: :razz:

已登录用户不需要填写以下内容

目前评论:1   其中:访客  0   博主  0

    • avatar 3:30 重庆市 移动 2

      对小白真的很友好,写的很全面。