1
0

BxBaseEmbedMicrolink.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Microlink integration.
  11. *
  12. * @see BxDolEmbed
  13. */
  14. class BxBaseEmbedMicrolink extends BxTemplEmbedSystem
  15. {
  16. protected $_sEndpoint;
  17. protected $_sEndpointPro;
  18. public function __construct ($aObject, $oTemplate)
  19. {
  20. parent::__construct ($aObject, $oTemplate);
  21. $this->_bAsync = true;
  22. $this->_sEndpoint = "https://api.microlink.io/?url=";
  23. $this->_sEndpointPro = "https://pro.microlink.io/?url=";
  24. }
  25. public function getDataFromApi ($sUrl, $sTheme)
  26. {
  27. $sResponse = '';
  28. if(($sApiKey = getParam('sys_embed_microlink_key')) != '')
  29. $sResponse = bx_file_get_contents($this->_sEndpointPro . $sUrl, [], 'get', ['x-api-key: ' . $sApiKey]);
  30. else
  31. $sResponse = bx_file_get_contents($this->_sEndpoint . $sUrl);
  32. $aResponse = [];
  33. if(!$sResponse || !($aResponse = json_decode($sResponse, true)) || empty($aResponse['data']) || !is_array($aResponse['data']))
  34. return '';
  35. $aResult = [
  36. 'title' => $aResponse['data']['title'],
  37. 'description' => $aResponse['data']['description'],
  38. 'image' => $aResponse['data']['image']['url'],
  39. 'logo' => $aResponse['data']['logo']['url'],
  40. 'url' => $sUrl,
  41. 'domain' => parse_url($sUrl, PHP_URL_HOST)
  42. ];
  43. $this->_storeImages($aResult);
  44. return json_encode($aResult);
  45. }
  46. }
  47. /** @} */