Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace CustommoduleReviewRatingBlock;
- class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
- {
- protected $_helper;
- protected $jsonHelper;
- public function __construct(
- MagentoFrameworkViewElementTemplateContext $context,
- array $data = [],
- CustommoduleReviewRatingHelperData $helper,
- MagentoFrameworkJsonHelperData $jsonHelper
- ) {
- parent::__construct($context, $data);
- $this->_helper = $helper;
- $this->jsonHelper = $jsonHelper;
- }
- .................
- public function get_reviews_data( $url ){
- $dataResponse = $this->getDataFromApi($url);
- return $dataResponse['data']['reviews'];
- }
- public function getDataFromApi($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- $data = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- $decodedData = $this->jsonHelper->jsonDecode($data);
- return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
- }
- $url = "http://website.com/api?url";
- $reviewsData = $this->get_reviews_data( $url );
- echo "<pre>";
- print_r( $reviewsData );
- echo "</pre>";
- ?>
- public function getDataFromApi($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- $data = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if(is_array($data) && !empty($data)) {
- $decodedData = $this->jsonHelper->jsonDecode($data);
- }else {
- $data = (array) $data;
- $decodedData = $this->jsonHelper->jsonDecode($data);
- }
- return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
- }
Add Comment
Please, Sign In to add comment