Home

Tuesday, January 8, 2013

How to get all products information with JSON format in magento?

Step 1 : Create Custom API page in root folder for ex.(mg_api.php)

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('*');
$i = 0;
$product_data = array();
foreach ($collection as $product)
{                       
   $product_data[$i]=array("product_id" => $product->getId(),
                    "name" => $product->getName(),
                    "sku" => $product->getSku(),
                    "price" => $product->getPrice(),
                    "url" => $product->getProductUrl(),
                    "thumb" => $product->getThumbnailUrl(),
                    "small" => $product->getSmallImageUrl(),
                    "big" => $product->getImageUrl()
                    );
    $i++;
}
echo json_encode(array('data' => $product_data));
?>

Step 2 : Fetch data from Custom API page using CURL

  $url = 'http://localhost/examples/magento_161/mg_api.php';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
  $data = json_decode($result, true);

  echo "<pre>";
  print_r($data);
  echo "</pre>";

No comments:

Post a Comment

rathoddhirendra.blogspot.com-Google pagerank and Worth