PushBaidu.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class PushBaidu extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'push:baidu';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Push new to Baidu.';
  19. /**
  20. * Create a new command instance.
  21. *
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. }
  27. protected function push($urls)
  28. {
  29. $api = config('vienblog.baidu.manual_push.api');
  30. $ch = curl_init();
  31. $options = array(
  32. CURLOPT_URL => $api,
  33. CURLOPT_POST => true,
  34. CURLOPT_RETURNTRANSFER => true,
  35. CURLOPT_POSTFIELDS => implode("\n", $urls),
  36. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  37. );
  38. curl_setopt_array($ch, $options);
  39. $result = curl_exec($ch);
  40. echo $result;
  41. return $result;
  42. }
  43. /**
  44. * Execute the console command.
  45. *
  46. * @return mixed
  47. */
  48. public function handle()
  49. {
  50. dump(config('vienblog.baidu.manual_push.open'));
  51. if(!config('vienblog.baidu.manual_push.open')) {
  52. dump('未开启主动推送,请在config/vienblog.php中的baidu选项中根据注释内容配置,开启主动推送需要先配置domain和api');
  53. return;
  54. }
  55. $domain = config('vienblog.baidu.manual_push.domain');
  56. $articles = DB::select("select id, slug from blog_articles");
  57. $tags = DB::select("select id, tag_name from blog_tags");
  58. $categories = DB::select("select id, cate_name from blog_categories");
  59. $postedUrls = DB::table("baidu_posted_urls")->get()->map(function ($value) {
  60. return (array)$value;
  61. })->toArray();
  62. $postedUrlsArray = array();
  63. foreach ($postedUrls as $postedUrl) {
  64. array_push($postedUrlsArray, $postedUrl['url']);
  65. }
  66. // $postedUrls = json_decode(json_encode($postedUrls), true);
  67. $urls = array($domain);
  68. foreach ($articles as $article) {
  69. $url = $domain.'/'.$article->slug;
  70. array_push($urls, $url);
  71. }
  72. $tagUrls = array();
  73. foreach ($tags as $tag) {
  74. $url = $domain.'/tag/'.$tag->tag_name;
  75. array_push($tagUrls, $url);
  76. }
  77. $catUrls = array();
  78. foreach ($categories as $category) {
  79. $url = $domain.'/category/'.$category->cate_name;
  80. array_push($catUrls, $url);
  81. }
  82. $finalUrls = array_diff(array_merge($tagUrls, $catUrls, $urls), $postedUrlsArray);
  83. if (count($finalUrls) == 0) {
  84. $this->info('no new url needs to post.');
  85. return;
  86. }
  87. dump($finalUrls);
  88. $res = $this->push($finalUrls);
  89. $res = json_decode($res);
  90. dump($res);
  91. if (isset($res->success)) {
  92. $doneUrls = array();
  93. foreach ($finalUrls as $url) {
  94. array_push($doneUrls, ['url' => $url]);
  95. }
  96. DB::table('baidu_posted_urls')->insert($doneUrls);
  97. dump('success');
  98. }
  99. if(isset($res->error)) {
  100. dump('error: ');
  101. dump($res->error);
  102. }
  103. }
  104. }