eBay API PHP 错误未定义索引


ebay apis php error undefined index

我正在为一个大学项目使用eBay API实现一个不同的简单搜索引擎来ebay项目。我有一个简单的索引.php文件,其中包含一个搜索框:

<form action="MySample1.php" method="POST">
    <input type="text" name="query" size="90" maxlength="255" />
    <input type="submit" value="Cerca" /> 
</FORM>

以及用于显示结果的示例.php文件。示例.php文件实现了一个函数 findItemsByKeywordsResults,如下所示:

<?php
session_start();
error_reporting(E_ALL);  // Turn on all errors, warnings and notices for easier debugging
// API request variables
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  // URL to call
$s_endpoint = "http://open.api.ebay.com/shopping?";  // Shopping URL to call
$m_endpoint = 'http://svcs.ebay.com/MerchandisingService?';  // Merchandising URL to call
$version = '1.0.0';  // API version supported
$appid = 'ed-7877-4e90-a4b4-bc6d897f71c3';  // Generated via My Account to ebay developer
$globalid = 'EBAY-US';  // Global ID of the eBay site you want to search (e.g., EBAY-DE)
$responseEncoding = 'XML';  // Type of response we want back
$cellColor = "bgcolor='"#dfefff'"";  // Light blue background used for selected items
$_SESSION['query'] = $_POST['query'];
function findItemsByKeywordsResults($selectedItemID = '', $cellColor = '') {
    $query= $_SESSION['query'];
    $safequery = urlencode( $query);  // Make the query URL-friendly
    global $endpoint;
    global $appid;
    global $responseEncoding;
    global $version;
    global $globalid;
    global $sort;
    $results = "<FORM ACTION='"" . $_SERVER['PHP_SELF'] . "'" METHOD='"POST'"> 'n";
    $results .= "<FIELDSET>'n<LEGEND>Sort by: </LEGEND>'n";
    $results .= "<SELECT NAME='"Order'" onchange='"this.form.submit();'"> 'n";
    $results .= "<OPTION VALUE='"BestMatch'" SELECTED='"selected'"> Best Match </OPTION> 'n";
    $results .= "<OPTION VALUE='"BidCountFewest'" > Bid Count Ascending </OPTION>  'n";
    $results .= "<OPTION VALUE='"BidCountMost'"> Bid Count Descending </OPTION>  'n";
    $results .= "<OPTION VALUE='"PricePlusShippingLowest'" > Price + Shipping Ascending </OPTION>  'n";
    $results .= "<OPTION VALUE='"PricePlusShippingHighest'"> Price + Shipping Descending </OPTION>  'n";
    $results .= "</SELECT> 'n </FIELDSET> 'n";
    $results .= "</FORM> 'n";  
    // 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 .= "&sortOrder=$sort";
    $apicall .= "&keywords=$safequery";
    $apicall .= "&paginationInput.entriesPerPage=10";
    // 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
        $results .= "<h2>These are the results for  <i>". $_SESSION['Query'] ."</i> </h2>'n";
        $results .= "<!-- start table in findItemsByKeywordsResults --> 'n";
        $results .= "<table width='"60%'" cellpadding='"5'" border='"1'">";
        //for each item node build a table cell and append it to $results
        foreach($resp->searchResult->item as $item){
            // Set the cell color blue for the selected most watched item
            if ($selectedItemID == $item->itemId) {
                $thisCellColor = $cellColor;
            } else {
                $thisCellColor = '';
            }
            // Determine which price to display
            if ($item->sellingStatus->currentPrice) {
                $price = $item->sellingStatus->currentPrice;
            } else {
                $price = $item->listingInfo->buyItNowPrice;
            }
            // For each item, create a cell with imageURL, viewItemURL, watchCount, currentPrice
            $results .= "<tr> 'n<td $thisCellColor valign='"top'"> 'n";
            $results .= "<img src='"$item->galleryURL'"></td> 'n";
            $results .= "<td $thisCellColor valign='"top'"> <p><a href='"" . $item->viewItemURL . "'">" . $item->title . "</a></p>'n";
            $results .= 'Shipped to: <b>' . $item->shippingInfo->shipToLocations . "</b><br> 'n";
            $results .= 'Current price: <b>$' . $price . "</b><br><br> 'n";
            $results .= "<FORM ACTION='"" . $_SERVER['PHP_SELF'] . "'" METHOD='"POST'"> 'n";
            $results .= "<INPUT TYPE='"hidden'" NAME='"Selection'" VALUE='"$item->itemId'"> 'n";
            $results .= "<INPUT TYPE='"submit'" NAME='"$item->itemId'" ";
            $results .= "VALUE='"Get Details and Similar Items'"> 'n";
            $results .= "</FORM> 'n";
            $results .= "</td> </tr> 'n'n";
        }
        $results .= "</tr></table> 'n<!-- finish table --> 'n";
    }
    // 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>";   
    }
    return $results;
}//end of the function

好吧,最初我得到了正确的结果。 当我尝试使用"排序依据"表单对结果重新排序并重新加载页面时,问题就来了。我发现的错误是:

注意:未定义的索引:查询在 C:''xampp''htdocs''GettingStarted''Sample.php 第 23 行

你们有人可以帮我解决这个问题吗?

而不是下面的代码:

$_SESSION['query'] = $_POST['query'];

使用以下代码:

$_SESSION['query'] = isset($_POST['query']) ? $_POST['query'] : (isset($_SESSION['query']) ?$_SESSION['query'] : '' );
$_POST['query']; is not set, you are submitting the form without filling query