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.