0

I need to convert the response from magento SOAP API to json, How to achieve this, Here is the code

$proxy = new SoapClient('http://magentohost/api/v2_soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey'); 
$result = $proxy->catalogProductList($sessionId);
var_dump($result);
1
1

If you want to send json data back from magento end you need to overwrite each function in magento API

For Example catalogProductList() method

Copy file

app/code/core/Mage/Catalog/Model/Product/Api.php

To

app/code/local/Mage/Catalog/Model/Product/Api.php

And modify below function

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    /** @var $apiHelper Mage_Api_Helper_Data */
    $apiHelper = Mage::helper('api');
    $filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
    try {
        foreach ($filters as $field => $value) {
            $collection->addFieldToFilter($field, $value);
        }
    } catch (Mage_Core_Exception $e) {
        $this->_fault('filters_invalid', $e->getMessage());
    }
    $result = array();
    foreach ($collection as $product) {
        $result[] = array(
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
            'category_ids' => $product->getCategoryIds(),
            'website_ids'  => $product->getWebsiteIds()
        );
    }
    return Mage::helper('core')->jsonEncode($result);    
}
9
  • Thanks for the response.. I have added the code but no data is fetched the output i am getting is array(0) { } Feb 29 '16 at 11:56
  • You have products in your shop? Feb 29 '16 at 12:01
  • yes.. The output of the default code is array(1429) { [0]=> Feb 29 '16 at 12:03
  • Copy original function items () from core file and just replace return statement Feb 29 '16 at 12:07
  • Getting same output Feb 29 '16 at 12:10
0

To decode JSON into an array you can use the following code:

$array = Mage::helper('core')->jsonDecode($result);

To encode an array into JSON you can use the following code:

$json = Mage::helper('core')->jsonEncode($result);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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