0

Getting products from curl request from other store but unable to save data in magento. I'm getting getModelInstance() fatal error. Here is my code

 <?php
 $mageFilename = '../app/Mage.php';
  require_once $mageFilename;

  Mage::setIsDeveloperMode(true);
   ini_set('display_errors', 1);


  umask(0);

    $curl = curl_init();

    $API_ID          = "3";
    $API_URL         = "http://magento.com/component/controller.php";
    $App_key         = "03254254";
    $Functionname    = "get_product_list_by_category_id_and_or_vendor_id";
    $data            = "data";


    $product_id='product_id';
    $name='name';
    $description='description';
    $meta_title='meta_title';
    $meta_description='meta_description';
    $meta_keyword='meta_keyword';
    $tag='tag';
    $model='model';
    $sku='sku';
    $upc='upc';
    $ean='ean';
    $jan='jan';
    $isbn='isbn';
    $mpn='mpn';
    $location='location';
    $quantity='quantity';



    curl_setopt_array($curl, array(
        CURLOPT_URL => $API_URL,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "Appid: 3",
            "Appkey: 03254254",
            "Cache-control: no-cache",
            "Categoryid: 101,102,103,104,105",
            "Functionname: get_product_list_by_category_id_and_or_vendor_id",
            "Limit: 1000",
            "Postman-token: 7e0cbcb4-ad69-7f38-de10-f2387f8477ee",
            "Start: 0",
            "Vendorid: 1474614118"
        ),
    ));

    $response = curl_exec($curl);



    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        $data = json_decode($response);
        $data = $data->data;
       // var_dump($data);
        foreach ($data->get_product_list_by_category_id_and_or_vendor_id as $item) {
            $product = Mage::getModel('catalog/product');

            if ($item->product_id) {
                $product->load($item->product_id);
            }

            $product->setSku($item->sku)
                ->setEan($item->ean)
                ->setProductId($item->product_id)
                ->setName($item->name)
                ->setDescription($item->description)
                ->setKeyword($item->keyword)
                ->setPrice($item->Price)
                ->setQtr($item->Quantity)
                ->setS_descriotion($item->S_description)
                ->setStatus($item->Status)
                ->setVisibility($item->Visibility)
                ->SetWeight($item->Weight)
                // Continue setting all other attribute values
                ->save();


            $stockItem = $product->getStockItem();
            $stockItem->setQty($item->quantity)
                ->setIsInStock($item->quantity > 0)
                ->save();

            /*$result[] =array(
                'id'            => $product->setId()->setData,
                'sku'           => $product->setSku()->setData,
                'name'          => $product->setName()->setData,
                'Status'        => $product->setStatus()->setData,
                'description'   => $product->setDescription()->setData,
                'weight'        => $product->setWeight()->setData,
                'created at'    => $product->setCreatedAt()->setData,
                /*'Price'         => Mage::helper('core')->currency($product->setPrice(), true, false), //." ".$currencyCode,
                'Special_Price' => Mage::helper('core')->currency($product->setSpecialPrice(), true, false), //." ".$currencyCode,
                'Stock'         => $product->setQty()->setData,
                'hasOptions'    => $product->isConfigurable()->setData,
                'image'         => $product->setImageUrl()->setData,
                'category'      => $product->setCategoryIds()->setData,
                'meta_title'    => $product->setMetaTitle()->setData,
                'meta_desc'     => $product->setMetaDescription()->setData,
                'meta_keyword'  => $product->setMetaKeyword()->setData,
                'Reviews'       => $product->setReviewsSummaryHtml()->setData,
                'categories'    => $product->setCategoryNames()->setData,
                'Made-in'       => $product->setCountryOfManufacture()->setData,
                'brand'         => $product->setBrand()->setData,

            );*/



        }

    }
2

Short answer.
Add after umask(0); (or before it) this Mage::app();

Long answer (may help on future problems)

I've searched for the text getModelInstance in the code and the only occurrence is in the Mage_Core_Model_Config class.
It is used in 3 locations.
2 of them are called from inside the class itself with $this->getModelInstance(). This cannot be the problem because you cannot have $this to be null if you are already inside the class.

The other place is when calling Mage::getModel() and it is called like this self::getConfig()->getModelInstance($modelClass, $arguments);.
This means that Mage::getConfig() returns null for some reason.
The method getConfig only returns self::$_config.

Checking where this config is initialized, you will see that you have to run Mage::app() or Mage::init() or Mage::run() to make it have a value.

| improve this answer | |

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.