顯示包含「ebay」標籤的文章。顯示所有文章
顯示包含「ebay」標籤的文章。顯示所有文章

2011/12/09

Ebay API SDK Add Item

從今天起,進入api開發的code階段。 這個階段預計有三部分,上傳產品,更新產品,刪除產品。每個部分都以固定價格的產品為例,其他如訂單管理,可變價格產品的管理等,需要讀者自己延伸。 因為每個人編碼習慣不同,所以編寫code的方式也不盡相同,但是為了便於大家更好的理解,這裡的例子都會盡量簡單一些。 對於不同種類的產品,參數可能會大致不相同,但基本的設置無外乎就這些。對於Variation的產品,比如一雙鞋,有43,42,44等尺寸這些產品,一定要仔細參照上邊的code是如何設置的,即便如此,也不能保證一次上傳就能成功,最好就是把返回信息$res保存起來,針對不同error,逐個解決。



class uploadProductToEbay {

 protected $_session = null;
 protected $_cs = null;
 protected $_logger = null;
 protected $_basedir = '';

    public function __construct()
    {
         require_once 'ebay/EbatNs_1_0_679/EbatNs_ServiceProxy.php';
  require_once 'ebay/EbatNs_1_0_679/EbatNs_Logger.php';
  require_once 'ebay/EbatNs_1_0_679/AddFixedPriceItemRequestType.php';
  require_once 'ebay/EbatNs_1_0_679/AddFixedPriceItemResponseType.php';

 
  $this->_basedir = Mage::getBaseDir();
  $this->_session = new EbatNs_Session(Mage::getBaseDir().'yourpath/ebay.config.php');
  $this->_cs      = new EbatNs_ServiceProxy($this->_session);
  $this->_logger  = new EbatNs_Logger();
    }
    public function addEbayProduct($product_data){//$product_data是从db里取得数据,比如产品title,只需在db里更改就可以了

                $req = new AddFixedPriceItemRequestType();
  $item = new ItemType();

  $item->SKU = $product_data['customlabel'];// 产品编号
  $item->Country = 'US';//国家
  $item->AutoPay = true;//paypal支付的话,如果你不想有损失,就老老实实设为true
  $item->Description = trim($product_data['*description']);//描述,不能有ebay规定的敏感字符
  $item->ListingDuration = $product_data['*duration'];
  $item->Title = $product_data['title'];//需少于80字符,不能有ebay规定的敏感字符
  $item->Currency = 'USD';//币种
  $item->ListingType = 'FixedPriceItem';//固定价格产品
  $item->SubTitle = $product_data['subtitle'];
  $item->StartPrice = $product_data['*startprice'];
  $item->ConditionID = $product_data['conditionid'];
  $item->Quantity = round($product_data['*quantity']) ;
  $item->Location = $product_data['*location'];//卖家邮编
 
  $item->DispatchTimeMax = $product_data['*dipatch_time_max'];//最大耗费时间
  $item->PaymentMethods = 'PayPal';//支付方式
  $item->PayPalEmailAddress = $product_data['paypal_email_address'];//paypal账号
 
  $pdts  = new PictureDetailsType();//产品图片部分
  $pdts->GalleryType = $product_data['gallery_type'];
  if(preg_match('/https/',$product_data['picurl']))
   $product_data['picurl'] = str_replace('https','http',$product_data['picurl']);
  $pdts->PictureURL = $product_data['picurl'];
  $item->PictureDetails = $pdts;
 
  $item->PrimaryCategory = new CategoryType();
  $item->PrimaryCategory->CategoryID = $product_data['category'];//分类id
  $item->Site = 'US';
 
  $sdt  = new ShippingDetailsType();
  $ssot = new ShippingServiceOptionsType();
  $issot = new InternationalShippingServiceOptionsType();
  $stt  = new SalesTaxType();
 
  $sdt->ShippingType = 'Flat';
  $sdt->PromotionalShippingDiscount = $product_data['promotional_shipping_discount'];
  $sdt->InternationalPromotionalShippingDiscount = $product_data['intl_promotional_shipping_discount'];
  $sdt->ShippingDiscountProfileID = $product_data['shipping_discount_profile_id'];
  $sdt->InternationalShippingDiscountProfileID = $product_data['intl_shipping_discount_profile_id'];
  $stt->SalesTaxState = $product_data['SalesTaxState'];
  $stt->SalesTaxPercent = $product_data['SalesTaxPercent'];
  $sdt->SalesTax = $stt;
 
  //第一种shipping设置
  $ssot->ShippingServicePriority = round($product_data['shipping1_priority']);
  $ssot->ShippingService = $product_data['shipping1_option'];
  $ssot->ShippingServiceCost = $product_data['shipping1_cost'];
  $sdt->ShippingServiceOptions[] = $ssot ;
 
  //第二种shipping设置,还可以有更多种
  $ssot2 = new ShippingServiceOptionsType();
  $ssot2->ShippingServicePriority = round($product_data['shipping2_priority']);
  $ssot2->ShippingService = $product_data['shipping2_option'];
  $ssot2->ShippingServiceCost = $product_data['shipping2_cost'];
  $sdt->ShippingServiceOptions[] = $ssot2 ;

  //第一种国际的shipping设置,主要针对美国以外的国家
  $issot->ShippingServicePriority = round($product_data['intlshipping1_priority']);
  $issot->ShippingService = $product_data['intlshipping1_option'];
  $issot->ShippingServiceCost = $product_data['intlshipping1_cost'];
  $issot->ShippingServiceAdditionalCost = $product_data['intlshipping1_addition'];
  $issot->ShipToLocation = $product_data['intlshipping1_location'];
  $sdt->InternationalShippingServiceOption[] = $issot ;
 
  //第二种国际的shipping设置,还可以有更多种
  $issot2 = new InternationalShippingServiceOptionsType();
  $issot2->ShippingServicePriority = round($product_data['intlshipping2_priority']);
  $issot2->ShippingService = $product_data['intlshipping2_option'];
  $issot2->ShippingServiceCost = $product_data['intlshipping2_cost'];
  $issot2->ShippingServiceAdditionalCost = $product_data['intlshipping2_addition'];
  $issot2->ShipToLocation = $product_data['intlshipping2_location'];
  $sdt->InternationalShippingServiceOption[] = $issot2 ;
 
  $item->ShippingDetails = $sdt;
 
  //退换货设置
  $rpt  = new ReturnPolicyType();
  $rpt->ReturnsAcceptedOption = $product_data['*return_accepted_option'];//如ReturnsAccepted 表示接受退换货
  $rpt->RefundOption = $product_data['refund_option'];//如MoneyBack 以现金方式返还
  $rpt->ReturnsWithinOption = $product_data['returns_with_option'];//如Days_30 30天内有效
  $rpt->Description = $product_data['additional_details'];//一些备注
  $rpt->ShippingCostPaidByOption = $product_data['shipping_cost_paid_by'];//如Buyer 购买者承担退货运费
  $item->ReturnPolicy = $rpt;
 
  $lcrpt = new ListingCheckoutRedirectPreferenceType();
  $lcrpt->SellerThirdPartyUsername = $product_data['seller_third_party_username'];
  $item->ListingCheckoutRedirectPreference = $lcrpt;
 
  $item->ShipToLocations = $product_data['ShipToLocations'];//如Worldwide,可送往世界各国
 

  //一些产品属性,如适合对象:男士,质地:棉  等等
  $nvlat_si = new NameValueListArrayType();
  $specific = unserialize($product_data['specific']);
  foreach ($specific as $k=>$v){
   $nvlt_si  = new NameValueListType();
   $nvlt_si->Name = $k;
   $nvlt_si->setValue($v);
   $nvlat_si->NameValueList[] = $nvlt_si;
  }
  $item->ItemSpecifics = $nvlat_si;
 
  /* *
  *
  *处理关联产品
  *
  */
  if($product_data['relationship'] == "Variation"){
 
   $vt    = new VariationsType();
   //$v     = new VariationType();
   $nvlat = new NameValueListArrayType();
   $nvlt  = new NameValueListType();
 
   $nvlt->Name = $product_data['Specification'];//如 Bracelet Length

   //$size_array如array('6.25 inches','6.50 inches','6.75 inches')
   $size_array = explode(';',$product_data['relationship_detail']);
   $i=1;
   foreach ($size_array as $size){
    $nvlt->setValue($size,$i++);
   }
   $nvlat->NameValueList = $nvlt;
   $vt->VariationSpecificsSet = $nvlat;
 
   //不同尺码的产品信息
   $relation = unserialize($product_data['relation']);
   foreach ($relation as $pd){
    $v     = new VariationType();
    $nvlat = new NameValueListArrayType();
    $nvlt  = new NameValueListType();
   
    $v->SKU = $pd['sku'];//产品编号
    $v->Quantity = $pd['qty'];//库存
    $v->StartPrice = $pd['price'];
    $nvlt->Name = $product_data['Specification'];
    $nvlt->setValue($pd['size']);//产品尺寸,颜色等属性
    $nvlat->setNameValueList($nvlt);
    $v->VariationSpecifics = $nvlat;
    $vt->Variation[] = $v;

   }
  }

  $item->Variations = $vt;
  $req->Item = $item;
  $res = $this->_cs->AddFixedPriceItem($req);//$res为返回信息,成功,失败,失败原因等


  return $res->ItemID;
    }
}

轉自http://blog.csdn.net/shangxiaoxue/article/details/6949037

2011/02/28

Make HTTP / XML request in API and Parse result (PHP)

for美國郵政局,using simple XML request, include the XML within the url with the registered userID,
it will then return XML result in similar format
http://www.usps.com/webtools/htm/Track-Confirm.htm

Test Request #1

http://SERVERNAME/ShippingAPITest.dll?API=TrackV2&XML=

<TrackRequest USERID="xxxxxxxx">
<TrackID ID="EJ958083578US">
</TrackID>
</TrackRequest>

Example
$xml ='http://testing.shippingapis.com/ShippingAPITest.dll?API=TrackV2&XML=';
$xml.= '<TrackRequest USERID="'.$usps_id.'">';
$xml.= '<TrackID ID="'.$usps_track_id.'">';
$xml.= '</TrackID></TrackRequest>';

$xml_file = urlencode($xml);

    if( ! $xml = simplexml_load_file($xml_file) )
    { 
        echo 'unable to load XML file';
    }
    else
    { 
        foreach( $xml as $user )
        {
            echo 'TrackSummary: '.$user->TrackSummary.'<br />';
for ($i=0;$i<count($user->TrackDetail);$i++){
echo 'TrackDetail: '.$user->TrackDetail[$i].'<br />';
}
        }
    }


EBay
SOAP API  request can be posted by CURL which is an extension in PHP.
E.g. ebay api, using CURL,data in xml and header can be make into HTTP request to the specific URL

 $endpoint = 'https://api.ebay.com/ws/api.dll';
    // create the xml request that will be POSTed
    $xmlRequest  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
    $xmlRequest .= ".........";


    $session  = curl_init($endpoint);                       // create a curl session
    curl_setopt($session, CURLOPT_POST, true);              // POST request type
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($session, CURLOPT_POSTFIELDS, $xmlRequest); // set the body of the POST
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);    // return values as a string - not to std out
    $headers = array(
      'X-EBAY-API-COMPATIBILITY-LEVEL: 643',
      'X-EBAY-API-DEV-NAME: ...........
//input your required a/c info
      'X-EBAY-API-SITEID: 0',                                 // Site 0 is for US
      'Content-Type: text/xml;charset=utf-8'
    );
    curl_setopt($session, CURLOPT_HTTPHEADER, $headers);    //set headers using the above array of headers

    $responseXML = curl_exec($session);                     // send the request
    curl_close($session);

//    print_r($responseXML);

ebay (Alternative)

但以上的CODE太麻煩, 次次都要人手對番那串極長的xml request, 再insert對應不同api的parameter太費時了,  所以ebay 有sdk for php 給人下載, 名為PHP Toolkit with 527 Support
file名係EbatNs_1_0_527_0, 內置了所有api, 只要在config 輸入appid 等info便可即時調用

 $session = new EbatNs_Session('../EbatNsSamples/config/ebay.config.php');
 $cs = new EbatNs_ServiceProxy($session);
 // Set the Request type
 //#type $req GetSellerListRequestType
 $req = new GetSellerListRequestType();



PHP Toolkit Samples 有齊各api的sample code, copy & paste 改variable數值就可以了