Thanks to Aaron once again for sending in this sample!
As discussed in another thread, here is the complete code for
SynchInventory rendered with PHP. I found writing this function in
PHP to be incredibly difficult, much more so than any of the other
functions. A couple of bits of advice for anyone trying to do this:
1. Pre-test sample data extensively before running it through a PHP
script. I highly recommend SoapUI (http://soapui.org
), which Andy had
previously recommended in this forum. It's much, much easier to test
sample data that way.
2. Download nusoap.php and modify and require it, as described below,
even if you're using PHP 5 and nusoap is already enabled. I don't
know why, but otherwise it just won't work.
3. Make sure that any content you're sending has "&" changed to
"&", "<" changed to "<" and ">" changed to ">" (I didn't
have any trouble with ", ', or #, but these could cause errors as
well). Another possibility would be to wrap all the content in CDATA
tags, but I could never quite get this to work. This should be
obvious to anyone experienced in XML, but it's an easy point to miss
in the fog of testing.
Also note that I've initialized a bunch of variables and hard coded
others as strings, but you will likely choose to do this a bit
differently depending on your requirements.
Good luck, and please let me know about your experience using this and
if you have any ideas to improve this script!
*********
Download the nusoap file here: http://sourceforge.net/projects/nusoap/![]()
Make the following changes:
Line 3198 - Rename "soapclient" to "soapclient_xxx"
Line 7990 - Rename "soapclient" to "soapclient_xxx"
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>';
?>
If you have php-soap installed, soapclient will interfere with it. Apparently SoapClient isn't case sensitive. I now see why you are changing the name to soapclient_xxx. I uninstalled php-soap since I am using the modified nuspoap.php anyway.
Josh Axelman
www.axelman.biz
Ran and I get the error below, what am i doing wrong?
any help would be appreciated
Errorwsdl error: HTTP ERROR: The PHP cURL Extension is required for HTTPS or NLTM. You will need to re-build or update your PHP to included cURL.
Request
Response
It doesn't look like your server has cURL installed. If you are on a Fedora it is as easy as
yum install php5-curl
Then restart apache and you should be good.
Aaron and Greg,
First I would like to thank you for the sample you have provided. I don't see why the soapclient needs to be changed to soapclient_xxx.
In order for this to properly authenticate, I had to change SOAP-ENV to soapenv.
On line 712 of lib/nusoap.php change the line to '<soapenv:Envelope xmlns:soapenv="*http://schemas.xmlsoap.org/soap/envelope/*
" xmlns:web="*http://api.channeladvisor.com/webservices/
">'.*
That should work.
Josh Axelman, axelman.biz