dqtok88424 2015-03-28 11:45
浏览 66
已采纳

使用单项API从eBay获取多个项目会显示ERROR

I'm using eBay Single item API to get details for selected items. I know eBay have multiple-item API also but it will not give all the details I want.


I have a Var $gpd where I have item numbers. Then using explode to make Array $items

Then using foreach to run eBay Single item API for all items in Array.

Then added 20% profit and roundup the price.

Then making static PayPal button.


My code below works perfect mostly of the time. But sometimes it shows ONLY the 2 first items in Array and then shows the ERROR:

$xml=simplexml_load_string($response) or die("Error: Could not get item");

If I again reload the page then it shows all the items in Array and everything works perfectly.

Here is my code:

<?php
$gpd = '281616878215, 221694130228, 191506118709, 271806308878, 231451119156' ;
$items = explode(', ', $gpd);
$profit = "1.2";
function round_up ($value, $places=0) {
  if ($places < 0) { $places = 0; }
  $mult = pow(10, $places);
  return ceil($value * $mult) / $mult;
}

foreach($items as $item) {
$appID = 'HERE-NEED-TO-PUT-EBAY-APPID';
$exexex = $item;
$request = '<?xml version="1.0" encoding="utf-8"?>
            <GetSingleItemRequest xmlns="urn:ebay:apis:eBLBaseComponents" >
            <ItemID>'.$exexex.'</ItemID>
            <IncludeSelector>Details,ShippingCosts,ItemSpecifics,Variations</IncludeSelector>
            </GetSingleItemRequest>';
$callName = 'GetSingleItem';
$compatibilityLevel = 647;
$endpoint = "http://open.api.ebay.com/shopping";
$headers[] = "X-EBAY-API-CALL-NAME: $callName";
$headers[] = "X-EBAY-API-APP-ID: $appID";
$headers[] = "X-EBAY-API-VERSION: $compatibilityLevel";
$headers[] = "X-EBAY-API-REQUEST-ENCODING: XML";
$headers[] = "X-EBAY-API-RESPONSE-ENCODING: XML";
$headers[] = "X-EBAY-API-SITE-ID: 0";
$headers[] = "Content-Type: text/xml";

$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);

$response = curl_exec($curl);
$data = simplexml_load_string($response) ;

$AckResponse = $data->Ack ;

$xml=simplexml_load_string($response) or die("Error: Could not get item");
$itemvalueprice = (float)$data->Item->ConvertedCurrentPrice;
$itemprice = $itemvalueprice * $profit;
$price = round_up ($itemprice, 1);
$paybutton = '<form name="_xclick" action="https://www.paypal.com/us/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick"><input type="hidden" name="business" value="my@paypal.com">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="item_name" value="' . htmlentities($data->Item->Title, ENT_QUOTES) . '">
<input type="hidden" name="amount" value="' . $price . '"><input type="hidden" name="on0" value="IMEI"><input type="text" name="os0" >
<br />
<input type="image" src="ordernow.jpg" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>' ;
$endt = str_replace(".000Z", "", $data->Item->EndTime);
$endtime = explode(T,$endt);
$startt = str_replace(".000Z", "", $data->Item->StartTime);
$starttime = explode(T,$startt);
$oldtrans = array('P','D','T','H','M','S');
$newtrans = array('<span class="red"> in </span>','d, ','','h ','m ','s');
$timeleft = str_replace($oldtrans,$newtrans,$data->Item->TimeLeft);


echo '<h2>' . htmlentities($data->Item->Title, ENT_QUOTES) . '</h2>
<span>Price <strong>' . $price . '</strong>$ </span>
<span>Sold <strong>' . $data->Item->QuantitySold . '</strong> </span>
<span>Score <strong>' . $data->Item->Seller-> PositiveFeedbackPercent . '</strong>% </span>
<span><strong>' . $data->Item->ListingStatus . '</strong></span>
<span><img src="' . $data->Item->GalleryURL . '" alt="' . htmlentities($data->Item->Title, ENT_QUOTES) . '" /></span>
<span>' . $paybutton . '</span><span>Expires ' . $endtime[0] . '' . $timeleft . '</span>
<br /><br />' ;
}
?>

My question is why does it show ERROR? If there is an ERROR why does it show all items most of the time? And even with ERROR it will always show the 2 first items in the array.

Any idea how to solve this?

Thank you for helping.

  • 写回答

1条回答 默认 最新

  • doutangu4978 2015-03-30 12:51
    关注

    There is no difference between GetSingle(Multiple)Items api call.

    The GetMultipleItems api call can take 20 items at one go.

    Try this:

    $appid = YOUR_APPID;
    $items = implode( ',', ITEM_ARRAY);
    
    // much simpler than using curl
    $xml = simplexml_load_file( "http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=YOUR&siteid=0&version=897&ItemID=$items&IncludeSelector=Details");
    

    You can get extra data by specifying the IncludeSelector parameter.

    http://developer.ebay.com/Devzone/shopping/docs/CallRef/GetMultipleItems.html#Request.IncludeSelector

    Getting the QuantitySold for each item:

    $ack = strtolower( ( string ) $xml->Ack );
    
    if( $ack == 'success' ) {
        foreach( $xml->Item as $item ) {
            echo "ItemID - " . $item->ItemID . '<br />';
            echo "QuantitySold - " . $item->QuantitySold . '<br />';
            echo '<br /><br />';
        }
    }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三向应力状态求剪应力
  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框
  • ¥15 网桥在转发帧时,会变帧的源地址和目的地址吗?
  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败