为什么我在PHP页面中收到“文档末尾的额外内容”?

时间:2012-09-26 01:21:56

标签: php amazon-web-services

这是我的错误:

  第1行第2行的

错误:文档末尾的额外内容

下面是第一个错误之前的页面呈现。

我刚从here复制了代码。

这是我的代码:

    <?php
header('Content-type: text/xml');
function awsRequest($searchIndex, $keywords, $responseGroup = false, $operation = "ItemSearch", $pageNumber = 1){
    $service_url = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService";
    $associate_tag = "your-associate-tag";
    $secret_key = "YOUR_SECRET_KEY";
    $access_key = "YOUR_ACCESS_KEY";

    // build initial request uri
    $request = "$service_url&Operation=$operation&AssociateTag=$associate_tag&SearchIndex=$searchIndex&Keywords=".urlencode($keywords)."&ItemPage=$pageNumber";

    // parse request into params
    $uri_elements = parse_url($request);
    $request = $uri_elements['query'];
    parse_str($request, $parameters);

    // add new params
    $parameters['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");
    $parameters['Version'] = $version;
    $parameters['AWSAccessKeyId'] = $access_key;
    if($responseGroup){
        $parameters['ResponseGroup'] = $responseGroup;
    }
    ksort($parameters);

    // encode params and values
    foreach($parameters as $parameter => $value){
        $parameter = str_replace("%7E", "~", rawurlencode($parameter));
        $value = str_replace("%7E", "~", rawurlencode($value));
        $request_array[] = $parameter . '=' . $value;
    }
    $new_request = implode('&', $request_array);

    // make it happen
    $signature_string = "GET\n{$uri_elements['host']}\n{$uri_elements['path']}\n{$new_request}";
    $signature = urlencode(base64_encode(hash_hmac('sha256', $signature_string, $secret_key, true)));

    // return signed request uri
    return "http://{$uri_elements['host']}{$uri_elements['path']}?{$new_request}&Signature={$signature}";
}
// make the request
$xml = simplexml_load_file(awsRequest("VideoGames", "call of duty", "Images", "ItemSearch", "1"));

// now retrieve some data
$totalPages = $xml->Items->TotalPages;
echo "<p>There are $totalPages pages in the XML results.</p>";

// retrieve data in a loop
echo "<ul>\n";
foreach($xml->Items->Item as $item){
    echo "<li>".$item->ASIN."</li>\n";
}
echo "</ul>\n";
?>

我正在AWS上部署它。

1 个答案:

答案 0 :(得分:0)

确保您的代码编辑器在文档中不包含BOM。我知道记事本++和其他一些默认包含这个的事实。如果包含它,它将在您的文档中插入不可见的字符,这看起来像输出到服务器,导致您看到的错误消息。尝试将代码复制并粘贴到没有BOM的新文档中。

在没有BOM(字节顺序标记)的Dreamweaver(或其他编辑器)中创建新的php文档应该可以解决问题。