Normal.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace addons\baidupush\library\push\driver;
  3. use addons\baidupush\library\push\Driver;
  4. use fast\Http;
  5. /**
  6. * 普通收录
  7. */
  8. class Normal extends Driver
  9. {
  10. protected $options = [
  11. 'site' => '',
  12. 'token' => '',
  13. ];
  14. /**
  15. * 构造函数
  16. * @param array $options 参数
  17. * @access public
  18. */
  19. public function __construct($options = [])
  20. {
  21. if (!empty($options)) {
  22. $this->options['site'] = isset($options['site']) ? $options['site'] : '';
  23. $this->options['token'] = isset($options['token']) ? $options['token'] : '';
  24. }
  25. }
  26. /**
  27. * 推送实时链接
  28. * @param array $urls URL链接数组
  29. * @return bool
  30. */
  31. public function realtime($urls)
  32. {
  33. return $this->request($urls, 'urls');
  34. }
  35. /**
  36. * 推送历史链接
  37. * @param array $urls URL链接数组
  38. * @return bool
  39. */
  40. public function history($urls)
  41. {
  42. return $this->realtime($urls);
  43. }
  44. /**
  45. * 删除链接
  46. * @param array $urls URL链接数组
  47. * @return bool
  48. */
  49. public function delete($urls)
  50. {
  51. return $this->request($urls, 'del');
  52. }
  53. protected function request($urls, $type)
  54. {
  55. $url = "http://data.zz.baidu.com/{$type}?site={$this->options['site']}&token={$this->options['token']}";
  56. try {
  57. $options = [
  58. CURLOPT_HTTPHEADER => [
  59. 'Content-Type: text/plain'
  60. ]
  61. ];
  62. $ret = Http::sendRequest($url, implode("\n", $urls), 'POST', $options);
  63. if ($ret['ret']) {
  64. $json = (array)json_decode($ret['msg'], true);
  65. if (!$json || isset($json['error'])) {
  66. $this->setError($json['message']);
  67. return false;
  68. }
  69. $this->setData($json);
  70. return true;
  71. }
  72. } catch (\Exception $e) {
  73. $this->setError($e->getMessage());
  74. }
  75. return false;
  76. }
  77. }