0

Use curl in custom module Magento ver 1.9.x

In /var/www/html/magento193/app/design/frontend/base/default/template/reviewrating/rightsidebar.phtml

$url = "http://mywebsite.com/api/extentionReviews?key=s4o3mbe8key9";
$getJson = $this->getDataFromApi( $url );
$ApiData = Mage::helper('core')->jsonDecode( $getJson );

print_r( $getJson );

In /var/www/html/magento193/app/code/local/Custommodule/ReviewRating/Block/Rightsidebar.php

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);
    return ($httpcode>=200 && $httpcode<300) ? $data : false;
}

In /var/www/html/magento193/app/design/frontend/base/default/layout/reviewrating.xml

<reference name="right">
    <block type="reviewrating/rightsidebar" name="reviewrating_rightsidebar" beofre="-"  template="reviewrating/rightsidebar.phtml" />
</reference>

Output : I am still getting data in json format. i want it to be in array.

{   "success":true,
    "error":false,
    "error_code":"",
    "message":"Successfully listed."
    ..........  
}

1 Answer 1

1

In your file /var/www/html/magento193/app/design/frontend/base/default/template/reviewrating/rightsidebar.phtml , You are printing the wrong variable. Update your code as follows,

$url = "http://mywebsite.com/api/extentionReviews?key=s4o3mbe8key9";
$getJson = $this->getDataFromApi( $url );
$ApiData = Mage::helper('core')->jsonDecode( $getJson );
print_r( $ApiData );
1
  • ok , let me correct it
    – inrsaurabh
    Commented Dec 6, 2017 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.