PushBaidu.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if(!config('vienblog.baidu.manual_push.open')) {
  51. dump('未开启主动推送,请在后台中配置domain和api');
  52. return;
  53. }
  54. $domain = config('vienblog.baidu.manual_push.domain');
  55. $articles = DB::select("select id, slug from blog_articles");
  56. $tags = DB::select("select id, tag_name from blog_tags");
  57. $categories = DB::select("select id, cate_name from blog_categories");
  58. $postedUrls = DB::table("baidu_posted_urls")->get()->map(function ($value) {
  59. return (array)$value;
  60. })->toArray();
  61. $postedUrlsArray = array();
  62. foreach ($postedUrls as $postedUrl) {
  63. array_push($postedUrlsArray, $postedUrl['url']);
  64. }
  65. // $postedUrls = json_decode(json_encode($postedUrls), true);
  66. $urls = array($domain);
  67. foreach ($articles as $article) {
  68. $url = $domain.'/'.$article->slug;
  69. array_push($urls, $url);
  70. }
  71. $tagUrls = array();
  72. foreach ($tags as $tag) {
  73. $url = $domain.'/tag/'.$tag->tag_name;
  74. array_push($tagUrls, $url);
  75. }
  76. $catUrls = array();
  77. foreach ($categories as $category) {
  78. $url = $domain.'/category/'.$category->cate_name;
  79. array_push($catUrls, $url);
  80. }
  81. $finalUrls = array_diff(array_merge($tagUrls, $catUrls, $urls), $postedUrlsArray);
  82. if (count($finalUrls) == 0) {
  83. $this->info('no new url needs to post.');
  84. return;
  85. }
  86. dump($finalUrls);
  87. $res = $this->push($finalUrls);
  88. $res = json_decode($res);
  89. dump($res);
  90. if (isset($res->success)) {
  91. $doneUrls = array();
  92. foreach ($finalUrls as $url) {
  93. array_push($doneUrls, ['url' => $url]);
  94. }
  95. DB::table('baidu_posted_urls')->insert($doneUrls);
  96. dump('success');
  97. }
  98. if(isset($res->error)) {
  99. dump('error: ');
  100. dump($res->error);
  101. }
  102. }
  103. }