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.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图