ShortyService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Common\Services;
  3. class ShortyService
  4. {
  5. private $host;
  6. public function __construct()
  7. {
  8. $this->host = config('services.short_app.url');
  9. }
  10. /**
  11. * 获取短地址url
  12. */
  13. public function getUrls($urls)
  14. {
  15. $host = $this->host . '/api/short';
  16. $headers['X-Requested-With'] = 'XMLHttpRequest';
  17. try {
  18. $ret = httpClient('post', $host, [
  19. "url" => $urls,
  20. "from" => "independent_web_".config('app.name')
  21. ], $headers);
  22. $ret = json_decode($ret, true);
  23. } catch (\Exception $e) {
  24. \Log::info($e->getMessage());
  25. return;
  26. }
  27. return $ret['data'] ?? [];
  28. }
  29. /**
  30. * 获取短地址url访问量
  31. */
  32. public function getUrlsHits($urls)
  33. {
  34. $host = $this->host . '/api/short-hits';
  35. $headers['X-Requested-With'] = 'XMLHttpRequest';
  36. try {
  37. $ret = httpClient('get', $host, [
  38. "url" => $urls,
  39. "from" => "independent_web_".config('app.name')
  40. ], $headers);
  41. $ret = json_decode($ret, true);
  42. } catch (\Exception $e) {
  43. \Log::info($e->getMessage());
  44. return;
  45. }
  46. return $ret['data'] ?? [];
  47. }
  48. }