Twitter.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Twitter
  4. *
  5. * This file is part of Grav MediaEmbed plugin.
  6. *
  7. * Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
  8. * http://benjamin-regler.de/license/
  9. */
  10. namespace Grav\Plugin\MediaEmbed\Services;
  11. use Grav\Plugin\MediaEmbed\OEmbed\OEmbedRich;
  12. /**
  13. * Twitter
  14. */
  15. class Twitter extends OEmbedRich
  16. {
  17. public function getOEmbed()
  18. {
  19. if ($this->oembed) {
  20. return $this->oembed;
  21. }
  22. $endpoint = $this->format($this->config->get('endpoint', ''));
  23. if (!$endpoint) {
  24. return [];
  25. }
  26. $response = \Requests::get($endpoint);
  27. if (!$response->success) {
  28. $response->throw_for_status();
  29. }
  30. $json = json_decode($response->body, true);
  31. $this->oembed = [
  32. 'type' => 'rich',
  33. 'author_name' => $json['author_name'],
  34. 'author_url' => 'https://twitter.com/' . $json['author_name'],
  35. 'provider_name' => 'Twitter',
  36. 'provider_url' => 'https://twitter.com/',
  37. 'url' => 'https://www.twitter.com/' . $this->embedCode,
  38. 'html' => $json['html'],
  39. ];
  40. return $this->oembed;
  41. }
  42. }