Magento external Api Module

Viewed 25

Good afternoon, I am making a module for Magento, I need help to connect it to an external API, essentially the channeladvisor API, and make some requests to this API, here my Module Code... I need to synchronize my inventory in channel advisor in my store made in magento

Config XML

    <?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/import.xsd">
    <entity name="CA_Import" label="Customer Group" model="Ibnab\Tutie\Model\Import\CustomerGroup" behaviorModel="Magento\ImportExport\Model\Source\Import\Behavior\Basic" />
</config>

Itegration XML

<integration name="TestIntegration">

    <resources>

        <!-- To grant permission to Magento_Log::online, its parent Magento_Customer::customer needs to be declared as well-->

        <resource name="Magento_Customer::customer" />

        <resource name="Magento_Log::online" />

        <!-- To grant permission to Magento_Sales::reorder, all its parent resources need to be declared--> 

        <resource name="Magento_Sales::sales" />

        <resource name="Magento_Sales::sales_operation" />

        <resource name="Magento_Sales::sales_order" />

        <resource name="Magento_Sales::actions" />

        <resource name="Magento_Sales::reorder" />

    </resources>

</integration>

i wanna implement this php consult in mi module:

PHP Code:

<?php

//Initialize test variables

$sku='zz-cf002-e';
$title='Game Set';
$subtitle='';
$shortdescription='Some Descriptive Text';
$description='More Descriptive Text';
$weight=1.1;
$total=14;
$cost=99;
$retailprice=99;
$startingprice=99;
$takeItprice=99;
$secondchanceofferprice=99;
$storeprice=99;
$classname='mahjong';
$attname1='Bullet_List';
$attvalue1='A list of bullet points';
$attname2='SHIPPING_BESTRATE';
$attvalue2='11.77';
$attname3='SHIPPING_BESTRATESERVICE';
$attvalue3='UPS Ground';
$image1='ZZ-CF002-E_a.jpg';
$image2='ZZ-CF002-E_b.jpg';
$image3='ZZ-CF002-E_c.jpg';
$image4='ZZ-CF002-E_d.jpg';
$usa_48_ups_ground=10;
$usa_48_ups_ground_2=7;
$usa_48_ups_2day=30;
$usa_48_ups_2day_2=15;

//Create the XML set

require_once('nusoap.php');

$client = new soapclient_xxx('https://api.channeladvisor.com/
ChannelAdvisorAPI/v1/inventoryService.asmx?WSDL', true );

$err = $client->getError();

if ($err)
{
echo 'Constructor error' . $err . '';
}


$developerKey = 'XXX';

$password = 'XXX';

$headers = '<APICredentials xmlns="http://api.channeladvisor.com/
webservices/">

       <DeveloperKey>'.$developerKey.'</DeveloperKey>

       <Password>'.$password.'</Password>

       </APICredentials> ';


 $arrData = array('accountID'=>'XXX',
 'item'=>array(
 'Sku'=>$sku,
 'Title'=>$title,
 'Subtitle'=>$subtitle,
 'ShortDescription'=>$shortdescription,
 'Description'=>$description,
 'Weight'=>$weight,

 'QuantityInfo'=>array(
       'UpdateType'=>'Absolute', 'Total'=>$total),

 'PriceInfo'=>array(
       'Cost'=>number_format($cost, 2),
       'RetailPrice'=>number_format($retailprice, 2),
       'StartingPrice'=>number_format($startingprice, 2),
         'ReservePrice'=>0,
       'TakeItPrice'=>number_format($takeItprice, 2),
       'SecondChanceOfferPrice'=>number_format($secondchanceofferprice, 2),
       'StorePrice'=>number_format($storeprice, 2)),

  'ClassificationInfo'=>array(
       'Name'=>$classname, 'AttributeList'=>array(
         'ClassificationAttributeInfo___1'=>array(
              'Name'=>$attname1, 'Value'=>$attvalue1),
         'ClassificationAttributeInfo___2'=>array(
               'Name'=>$attname2, 'Value'=>$attvalue2),
         'ClassificationAttributeInfo___3'=>array(
              'Name'=>$attname3, 'Value'=>$attvalue3))),

   'ImageList'=>array(
           'ImageInfoSubmit___1'=>array(
                  'PlacementName'=>'ITEMIMAGEURL1',
                 'FilenameOrUrl'=>$image1),
                       'ImageInfoSubmit___2'=>array(
                  'PlacementName'=>'ITEMIMAGEURL2',
                 'FilenameOrUrl'=>$image2),
                       'ImageInfoSubmit___3'=>array(
                  'PlacementName'=>'ITEMIMAGEURL3',
                 'FilenameOrUrl'=>$image3),
                   'ImageInfoSubmit___4'=>array(
                  'PlacementName'=>'ITEMIMAGEURL4',
                 'FilenameOrUrl'=>$image4)),

       'ShippingInfo'=>array(
               'DistributionCenterCode'=>'Alhambra', 'ShippingRateList' =>array(
                       'ShippingRateInfo___1'=>array(
                               'DestinationZoneName'=>'USA48',
                               'CarrierCode'=>'* UPS',
                               'ClassCode'=>'GROUND',
                               'FirstItemRate'=>$usa_48_ups_ground,
                               'AdditionalItemRate'=>$usa_48_ups_ground_2,
                               'FirstItemHandlingRate'=>0,
                               'AdditionalItemHandlingRate'=>0,
                               'FreeShippingIfBuyItNow'=>'false',
                               'FirstItemRateAttribute'=>'Price',
                               'AdditionalItemRateAttribute'=>'Price'),
                       'ShippingRateInfo___2'=>array(
                               'DestinationZoneName'=>'USA48',
                               'CarrierCode'=>'UPS',
                               'ClassCode'=>'2DAY',
                               'FirstItemRate'=>$usa_48_ups_2day,
                               'AdditionalItemRate'=>$usa_48_ups_2day_2,
                               'FirstItemHandlingRate'=>0,
                               'AdditionalItemHandlingRate'=>0,
                               'FreeShippingIfBuyItNow'=>'false',
                               'FirstItemRateAttribute'=>'Price',
                               'AdditionalItemRateAttribute'=>'Price'))),
  ));

//fix repeating fields prior to send (eliminate ___1, ___2, etc.)

function array2xml($arrData, $level) {
       $xml = '';
       foreach( $arrData as $key => $value ) {
               $spacer = '';
               for ($i = 0; $i < $level; $i++) { $spacer .= "\t"; }
               if ( is_array( $value ) ) {
                       if (preg_match("/^(.+)___\d+$/", $key, $m)) {
                               $xml .= $spacer . "<" . $m[1] . ">\n" . array2xml($value, $level
+1) . $spacer . "</" . $m[1] . ">\n";
                       } else {
                               $xml .= $spacer . "<" . $key . ">\n" . array2xml($value, $level
+1) . $spacer . "</" . $key . ">\n";
                       }
               } else {
                       $xml .= $spacer . "<" . $key . ">" . $value . "</" . $key . ">\n";
               }
       }
       return $xml;
}

//apply the function

$cleanedxml = '<SynchInventoryItem xmlns="http://
api.channeladvisor.com/webservices/">' . "\n" .
       array2xml($arrData, 0) .
       '</SynchInventoryItem>' . "\n";

// Call the SOAP method and send

$result = $client->call('SynchInventoryItem', $cleanedxml, false,
false, $headers);

//Print out the results

if ($client->fault)
{
echo 'Fault';
print_r($result);
echo '';
}
else
{
$err = $client->getError();

if ($err)
{
echo 'Error' . $err . '';
}
else
{
echo 'Result';
print_r($result);
echo '';
}

}

echo "<hr>";

echo '<h2>Request</h2>';

echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</
pre>';

echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</
pre>';

?>
0 Answers
Related