Guest User

Untitled

a guest
Dec 16th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php
  2. namespace CustommoduleReviewRatingBlock;
  3.  
  4. class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
  5. {
  6. protected $_helper;
  7. protected $jsonHelper;
  8.  
  9.  
  10. public function __construct(
  11. MagentoFrameworkViewElementTemplateContext $context,
  12. array $data = [],
  13. CustommoduleReviewRatingHelperData $helper,
  14. MagentoFrameworkJsonHelperData $jsonHelper
  15. ) {
  16. parent::__construct($context, $data);
  17.  
  18. $this->_helper = $helper;
  19. $this->jsonHelper = $jsonHelper;
  20. }
  21.  
  22. .................
  23.  
  24. public function get_reviews_data( $url ){
  25. $dataResponse = $this->getDataFromApi($url);
  26. return $dataResponse['data']['reviews'];
  27. }
  28.  
  29. public function getDataFromApi($url)
  30. {
  31.  
  32. $ch = curl_init();
  33. curl_setopt($ch, CURLOPT_URL, $url);
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  36. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  37. $data = curl_exec($ch);
  38. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  39. curl_close($ch);
  40. $decodedData = $this->jsonHelper->jsonDecode($data);
  41.  
  42. return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
  43.  
  44. }
  45.  
  46. $url = "http://website.com/api?url";
  47.  
  48. $reviewsData = $this->get_reviews_data( $url );
  49. echo "<pre>";
  50. print_r( $reviewsData );
  51. echo "</pre>";
  52. ?>
  53.  
  54. public function getDataFromApi($url)
  55. {
  56. $ch = curl_init();
  57. curl_setopt($ch, CURLOPT_URL, $url);
  58. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  60. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  61. $data = curl_exec($ch);
  62. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  63. curl_close($ch);
  64. if(is_array($data) && !empty($data)) {
  65. $decodedData = $this->jsonHelper->jsonDecode($data);
  66. }else {
  67. $data = (array) $data;
  68. $decodedData = $this->jsonHelper->jsonDecode($data);
  69. }
  70.  
  71. return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
  72.  
  73. }
Add Comment
Please, Sign In to add comment