├── README.md └── web-api.php /README.md: -------------------------------------------------------------------------------- 1 | magento-android-web-api 2 | ======================= 3 | 4 | This is a small module that adds the ability to remotely work with the Magento via the REST API 5 | 6 | More detail http://app-z.net 7 | 8 | 9 | Install
10 | 11 | 1. Copy web-api.php to root Magento folder
12 | 2. Set key parametr for access to functionality this script
13 | 14 | Examples of use 15 | * /web-api.php?route=feed/web_api/categories&parent=0&level=2&key=key1 16 | Return Categories tree information 17 | 18 | * /web-api.php?route=feed/web_api/products&category=4&key=key1 19 | Return Products list in category 20 | 21 | * /web-api.php?route=feed/web_api/product&id=800&key=key1 22 | Return product item 23 | 24 | * /web-api.php?route=feed/web_api/random&limit=4&key=key1 25 | Return Random Products list 26 | 27 | 28 | 29 | 30 | 31 | License 32 | ------- 33 | 34 | This software is distributed under the [GNU GPL V3](http://www.gnu.org/licenses/gpl.html) License. 35 | -------------------------------------------------------------------------------- /web-api.php: -------------------------------------------------------------------------------- 1 | getRequest()->getParam('key'); 24 | 25 | if ( !isset($key) || $key != WEB_API_KEY ) { 26 | 27 | $json = array('success' => false, 'code' => 20, 'message' => 'Invalid secret key'); 28 | print_r(json_encode($json)); 29 | 30 | 31 | }elseif( Mage::app()->getRequest()->getParam('route') == "feed/web_api/categories" ){ 32 | 33 | # -- $_GET params ------------------------------ 34 | $parent = Mage::app()->getRequest()->getParam('parent', 0); 35 | $level = Mage::app()->getRequest()->getParam('level', 1); 36 | # -- End $_GET params -------------------------- 37 | print_r(json_encode(getCategoryTree($parent, $level))); 38 | 39 | 40 | }elseif(Mage::app()->getRequest()->getParam('route') == "feed/web_api/products"){ 41 | 42 | # -- $_GET params ------------------------------ 43 | $category_id = Mage::app()->getRequest()->getParam('category', 0); 44 | # -- End $_GET params -------------------------- 45 | print_r(json_encode(products($category_id))); 46 | 47 | 48 | }elseif(Mage::app()->getRequest()->getParam('route') == "feed/web_api/product"){ 49 | 50 | # -- $_GET params ------------------------------ 51 | $product_id = Mage::app()->getRequest()->getParam('id', 0); 52 | # -- End $_GET params -------------------------- 53 | print_r(json_encode(product($product_id))); 54 | 55 | }elseif(Mage::app()->getRequest()->getParam('route') == "feed/web_api/random"){ 56 | 57 | # -- $_GET params ------------------------------ 58 | $limit = Mage::app()->getRequest()->getParam('limit', 4); 59 | # -- End $_GET params -------------------------- 60 | print_r(json_encode(random_products($limit))); 61 | } 62 | 63 | 64 | 65 | 66 | // 67 | // Random Products Items 68 | // 69 | // http://localhost/magento/web-api.php?route=feed/web_api/random&limit=4&key=key1 70 | // 71 | function random_products($limit){ 72 | $json = array('success' => true); 73 | 74 | $products = Mage::getModel('catalog/product')->getCollection(); 75 | $products->addAttributeToSelect(array('name', 'thumbnail', 'price')); //feel free to add any other attribues you need. 76 | 77 | Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products); 78 | Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); 79 | $products->getSelect()->order('RAND()')->limit($limit); 80 | 81 | foreach($products as $product){ 82 | $json['products'][] = array( 83 | 'id' => $product->getId(), 84 | 'name' => $product->getName(), 85 | 'href' => $product->getProductUrl(), 86 | 'thumb' => (string)Mage::helper('catalog/image')->init($product, 'thumbnail'), 87 | 'pirce' => Mage::helper('core')->currency($product->getPrice(), true, false) //." ".$currencyCode, 88 | ); 89 | } 90 | return $json; 91 | } 92 | 93 | 94 | // 95 | // Product Item 96 | // 97 | // http://localhost/magento/web-api.php?route=feed/web_api/product&id=800&key=key1 98 | // 99 | function product($product_id){ 100 | $json = array('success' => true); 101 | 102 | $product = Mage::getModel('catalog/product')->load($product_id); 103 | $json['product'] = array(); 104 | $json['product']['id'] = $product->getId(); 105 | $json['product']['name'] = $product->getName(); 106 | $json['product']['price'] = Mage::helper('core')->currency($product->getPrice(), true, false); 107 | $json['product']['description'] = $product->getDescription(); 108 | $json['product']['image'] = (string)Mage::helper('catalog/image')->init($product, 'image'); 109 | 110 | $mediaGallery = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages()->getItems(); 111 | $json['product']['images'] = array(); 112 | //loop through the images 113 | foreach ($mediaGallery as $image){ 114 | $json['product']['images'][] = $image['url']; 115 | } 116 | return $json; 117 | } 118 | 119 | 120 | // 121 | // Products in category 122 | // 123 | // http://localhost/magento/web-api.php?route=feed/web_api/products&category=4&key=key1 124 | // 125 | function products($category_id){ 126 | 127 | $json = array('success' => true, 'products' => array()); 128 | 129 | $category = Mage::getModel ('catalog/category')->load($category_id); 130 | 131 | $products = Mage::getResourceModel('catalog/product_collection') 132 | // ->addAttributeToSelect('*') 133 | ->AddAttributeToSelect('name') 134 | ->addAttributeToSelect('price') 135 | ->addFinalPrice() 136 | ->addAttributeToSelect('small_image') 137 | ->addAttributeToSelect('image') 138 | ->addAttributeToSelect('thumbnail') 139 | ->addAttributeToSelect('short_description') 140 | ->addUrlRewrite() 141 | ->AddCategoryFilter($category); 142 | 143 | Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); 144 | 145 | $currencyCode = Mage::app()->getStore()->getBaseCurrencyCode(); 146 | 147 | 148 | foreach($products as $product){ 149 | $json['products'][] = array( 150 | 'id' => $product->getId(), 151 | 'name' => $product->getName(), 152 | 'description' => $product->getShortDescription(), 153 | 'pirce' => Mage::helper('core')->currency($product->getPrice(), true, false), //." ".$currencyCode, 154 | 'href' => $product->getProductUrl(), 155 | 'thumb' => (string)Mage::helper('catalog/image')->init($product, 'thumbnail') 156 | ); 157 | } 158 | return $json; 159 | } 160 | 161 | 162 | 163 | // 164 | // Categories 165 | // 166 | // http://localhost/magento/web-api.php?route=feed/web_api/categories&parent=0&level=2&key=key1 167 | // 168 | function getCategoryTree( $parent = 0, $recursionLevel = 1 ) 169 | { 170 | if($parent == 0){ 171 | $parent = Mage::app()->getStore()->getRootCategoryId(); 172 | }else{ 173 | $parent = Mage::getModel('catalog/category')->load($parent)->getId(); 174 | } 175 | 176 | $tree = Mage::getResourceModel('catalog/category_tree'); 177 | /* @var $tree Mage_Catalog_Model_Resource_Category_Tree */ 178 | 179 | $nodes = $tree->loadNode($parent) 180 | ->loadChildren($recursionLevel) 181 | ->getChildren(); 182 | $tree->addCollectionData(null, false, $parent); 183 | 184 | $json = array('success' => true); 185 | 186 | $result = array(); 187 | 188 | foreach ($nodes as $node) { 189 | $result[] = array( 190 | 'category_id' => $node->getData('entity_id'), 191 | 'parent_id' => $parent, 192 | 'name' => $node->getName(), 193 | 'categories' => getNodeChildrenData($node)); 194 | } 195 | 196 | $json['categories'] = $result; 197 | return $json; 198 | } 199 | 200 | 201 | 202 | function getNodeChildrenData(Varien_Data_Tree_Node $node) 203 | { 204 | foreach ($node->getChildren() as $childNode) { 205 | $result[] = array( 206 | 'category_id' => $childNode->getData('entity_id'), 207 | 'parent_id' => $node->getData('entity_id'), 208 | 'name' => $childNode->getData('name'), 209 | 'categories' => getNodeChildrenData($childNode)); 210 | } 211 | return $result; 212 | } 213 | 214 | 215 | ?> 216 | --------------------------------------------------------------------------------