Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Thursday 4 June 2020

How to convert a multidimensional Array To XML in PHP

function createxmlele($data, $name='root', &$doc=null, &$node=null){
        if ($doc==null){
            $doc = new DOMDocument('1.0','UTF-8');
            $doc->formatOutput = TRUE;
            $node = $doc;
        }

        if (is_array($data)){
            foreach($data as $var=>$val){
                if (is_numeric($var)){
                    $this->createxmlele($val, $name, $doc, $node);
                }else{
                    if (!isset($child)){
                        $child = $doc->createElement($name);
                        $node->appendChild($child);
                    }

                    $this->createxmlele($val, $var, $doc, $child);
                }
            }
        }else{
            $child = $doc->createElement($name);
            $node->appendChild($child);
            $textNode = $doc->createTextNode($data);
            $child->appendChild($textNode);
        }

        if ($doc==$node){
            return $doc;
        }
    }

Tuesday 30 August 2016

How to connect WSDL file with XML data with Basic Authentication in php

HTTP Auth works with SOAP Client, However you cannot access password protected WSDL files 

This is Simple example to auth webservice using soapClient
<?php
ini_set('display_errors', 1);
/*$client = new SoapClient('Materials_Availability.wsdl');
var_dump($client->__getFunctions());*/
$wsdl = "http://localhost/test/soap_wsdl/stock.wsdl?wsdl";
$client = new SoapClient($wsdl, array('login' => "******", 'password' => "******"));
echo ''.print_r($client,true).'';
$result = $client->__getFunctions();
echo '<pre>';print_r($result);
//$file = file_get_contents('soapxml.xml');
//echo $file;exit;
$array = array(
    'REQUEST'=> array(
        'MATERIALS'=> array(
                0=>array(
                    'MATERIAL_NUMBER'=>123654,
                    'MATERIAL_NUMBER_TYPE'=>'JAY',
                    'REQUEST_TYPE'=>array(
                        'TYPE'=>1
                    )
                ),
                1=>array(
                    'MATERIAL_NUMBER'=>654789,
                    'MATERIAL_NUMBER_TYPE'=>'JAM',
                    'REQUEST_TYPE'=>array(
                        'TYPE'=>2
                    )
                )
            )
        )
    );
print_r($array);
$result = $client->getMaterialAvailability($array);
print_r($result);
exit;
?>

Wednesday 4 May 2016

Convert PHP, Magento, Joomla, Codeigniter, Cake PHP, Wordpress XML to JSON, XML to Array, JSON to Array

Convert PHP, Magento, Joomla, Codeigniter, Cake PHP, Wordpress XML to JSON, XML to Array, JSON to Array is very difficult task for some people. But, in PHP, believe me it’s very easy. One line of code each! Don’t believe? Just check out the code below and test it yourself!


$xml = simplexml_load_string($xmldata);
$json = json_encode($xml);
$array = json_decode($json,true);

Monday 28 September 2015

Creating xml file of all products fields in magento programmatically

I will be using Varien_Simplexml_Element class to read write xml nodes. The path to this class file is lib/Varien/Simplexml/Element.php

Here is a sample XML file which I am going to read through Magento code. I will also be adding an XML node to the following XML data.

$file = "products.xml";
        if (file_exists($file)) { unlink ($file); }
       
        $products = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('status', array('eq' => '1'))
                    ->addAttributeToFilter('type_id', array('eq' => 'simple'));
       
        //process your product collection as per your bussiness logic
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $productsX = $doc->createElement( "products" );
        $doc->appendChild( $productsX );
        foreach($products as $_product){

            $product = $doc->createElement( "product" );

            $sku = $doc->createElement( "sku" );
            $sku->appendChild(
             $doc->createTextNode($_product->getSku())
            );
            $product->appendChild($sku);

            $name = $doc->createElement("name");
            $name->appendChild(
             $doc->createTextNode( trim($_product->getName()) )
            );
            $product->appendChild($name);

            $image = $doc->createElement( "image" );
            $image->appendChild(
             $doc->createTextNode( trim($_product->getData('image')) )
            );
            $product->appendChild( $image );

            $smallimage = $doc->createElement( "smallimage" );
            $smallimage->appendChild(
             $doc->createTextNode( trim($_product->getData('small_image')) )
            );
            $product->appendChild( $smallimage );

            $thumbnail = $doc->createElement( "thumbnail" );
            $thumbnail->appendChild(
             $doc->createTextNode( trim($_product->getData('thumbnail')) )
            );
            $product->appendChild( $thumbnail );

            $urlkey = $doc->createElement( "urlkey" );
            $urlkey->appendChild(
             $doc->createTextNode( trim($_product->getData('url_key')) )
            );
            $product->appendChild( $urlkey );

            $shippingdelivery = $doc->createElement( "shippingdelivery" );
            $shippingdelivery->appendChild(
             $doc->createTextNode( trim($_product->getShippingDelivery()) )
            );
            $product->appendChild( $shippingdelivery );

            $shippingweight = $doc->createElement( "shippingweight" );
            $shippingweight->appendChild(
             $doc->createTextNode( trim($_product->getShippingWeight()) )
            );
            $product->appendChild( $shippingweight );

            $alu = $doc->createElement( "alu" );
            $alu->appendChild(
             $doc->createTextNode( trim($_product->getAlu()) )
            );
            $product->appendChild( $alu );

            $upsize = $doc->createElement( "upsize" );
            $upsize->appendChild(
             $doc->createTextNode( trim($_product->getUpsize()) )
            );
            $product->appendChild( $upsize );

            $price = $doc->createElement( "price" );
            $price->appendChild(
             $doc->createTextNode( trim($_product->getPrice()) )
            );
            $product->appendChild( $price );

            $specialprice = $doc->createElement( "specialprice" );
            $specialprice->appendChild(
             $doc->createTextNode( trim($_product->getSpecialPrice()) )
            );
            $product->appendChild( $specialprice );

            $color = $doc->createElement( "color" );
            $color->appendChild(
             $doc->createTextNode( trim($_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)))
            );
            $product->appendChild( $color );

            $status = $doc->createElement( "status" );
            $status->appendChild(
             $doc->createTextNode( trim($_product->getResource()->getAttribute('status')->getFrontend()->getValue($_product)) )
            );
            $product->appendChild( $status );

            $description = $doc->createElement( "description" );
            $description->appendChild(
             $doc->createTextNode( trim($_product->getDescription()) )
            );
            $product->appendChild( $description );

            $qty = $doc->createElement( "qty" );
            $qty->appendChild(
             $doc->createTextNode( trim(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()) )
            );
            $product->appendChild( $qty );

            $availability = $doc->createElement( "availability" );
            $availability->appendChild(
             $doc->createTextNode( trim(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock()) )
            );
            $product->appendChild( $availability );

            $productsX->appendChild($product); 
        }
        file_put_contents($file,$doc->saveXML(),FILE_APPEND);