chrisrubenstein avatar image
chrisrubenstein asked ·

FindItemsByKeywords - reloading with new keyword in PHP



How do you modify the $apicall variable with new keywords so that you can resubmit with different parameters. I've created a form beneath the results, just outside of the item loop to accept a new keyword, reassigned the variable $query. It's doesn't seem to work and I am stuck.


Here is the code. It's basically the tutorial provided by ebay for the finding API with a couple of modifications.


Thank you in advance.




error_reporting(E_ALL);  // Turn on all errors, warnings and notices for easier debugging


// API request variables

$endpoint = '';  // URL to call

$version = '1.0.0';  // API version supported by your application

$appid = '';  // Replace with your own AppID

$globalid = 'EBAY-US';  // Global ID of the eBay site you want to search (e.g., EBAY-DE)

$query = 'harry potter';  // You may want to supply your own query

$safequery = urlencode($query);  // Make the query URL-friendly

$i = '0';  // Initialize the item filter index to 0


// Create a PHP array of the item filters you want to use in your request

$filterarray =



    'name' => 'MaxPrice',

    'value' => '25',

    'paramName' => 'Currency',

    'paramValue' => 'USD'),


    'name' => 'FreeShippingOnly',

    'value' => 'true',

    'paramName' => '',

    'paramValue' => ''),


    'name' => 'ListingType',

    'value' => array('AuctionWithBIN','FixedPrice','StoreInventory'),

    'paramName' => '',

    'paramValue' => ''),



// Generates an indexed URL snippet from the array of item filters

function buildURLArray ($filterarray) {

  global $urlfilter;

  global $i;

  // Iterate through each filter in the array

  foreach($filterarray as $itemfilter) {

    // Iterate through each key in the filter

    foreach ($itemfilter as $key =>$value) {

      if(is_array($value)) {

        foreach($value as $j => $content) { // Index the key for each value

          $urlfilter .= "&itemFilter($i).$key($j)=$content";



      else {

        if($value != "") {

          $urlfilter .= "&itemFilter($i).$key=$value";






  return "$urlfilter";

} // End of buildURLArray function


// Build the indexed item filter URL snippet



// Construct the findItemsByKeywords HTTP GET call 

$apicall = "$endpoint?";

$apicall .= "OPERATION-NAME=findItemsByKeywords";

$apicall .= "&SERVICE-VERSION=$version";

$apicall .= "&SECURITY-APPNAME=$appid";

$apicall .= "&GLOBAL-ID=$globalid";

$apicall .= "&keywords=$safequery";

$apicall .= "&paginationInput.entriesPerPage=3";

$apicall .= "$urlfilter";


// Load the call and capture the document returned by eBay API

$resp = simplexml_load_file($apicall);


// Check to see if the request was successful, else print an error

if ($resp->ack == "Success") {

  $results = '';

  // If the response was loaded, parse it and build links  

  foreach($resp->searchResult->item as $item) {

    $pic   = $item->galleryURL;

    $link  = $item->viewItemURL;

    $title = $item->title;


    // For each SearchResultItem node, build a link and append it to $results

    $results .= "<tr><td><img src=\"$pic\"></td><td><a href=\"$link\">$title</a></td></tr>";




// If the response does not indicate 'Success,' print an error

else {

  $results  = "<h3>Oops! The request was not successful. Make sure you are using a valid ";

  $results .= "AppID for the Production environment.</h3>";






$results .= "<form action=\"" .  $_SERVER['PHP_SELF']. "\" method=\"post\">";

$results .= "<input type=\"text\" id=\"newkeywords\" name=\"newkeywords\"></input>";

$results .= "<input type=\"submit\" value=\"go\"></form>"; 

$query = $_POST'newkeywords'];






<!-- Build the HTML page with values from the call response -->



<title>eBay Search Results for <?php echo $query; ?></title>

<style type="text/css">body { font-family: arial,sans-serif;} </style>




<h1>eBay Search Results for <?php echo $query; ?></h1>





    <?php echo $results;?>







10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers

· Write an Answer

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.