自定义google表单函数返回“非法形成的XML语法”.(最后一行)- PHP脚本


Custom google sheets function returning "Illegally formed XML syntax. (Last line) - PHP script

我一直试图实现这个功能在谷歌表,但它一直返回"非法形成的xml语法"在第93行,最后一行"?>"。我附上了代码,没有我的个人信息。

我通过编码工具在谷歌表的代码编辑器粘贴此代码->脚本编辑器,有人知道我需要改变什么吗?

如果有人能帮助我找出我在哪里错了实现这个自定义函数代码到谷歌表,这将是非常感激!

代码开始:(对不起,我不知道如何正确输入)

<?php

// Region code and Product ASIN
$response = getAmazonPrice("com", "B00KQPGRRE");
function getAmazonPrice($region, $asin) {
$xml = aws_signed_request($region, array(
    "Operation" => "ItemLookup",
    "ItemId" => $asin,
    "IncludeReviewsSummary" => False,
    "ResponseGroup" => "Medium,OfferSummary",
));
$item = $xml->Items->Item;
$title = htmlentities((string) $item->ItemAttributes->Title);
$url = htmlentities((string) $item->DetailPageURL);
$image = htmlentities((string) $item->MediumImage->URL);
$price = htmlentities((string) $item->OfferSummary->LowestNewPrice->Amount);
$code = htmlentities((string) $item->OfferSummary->LowestNewPrice->CurrencyCode);
$qty = htmlentities((string) $item->OfferSummary->TotalNew);
if ($qty !== "0") {
    $response = array(
        "code" => $code,
        "price" => number_format((float) ($price / 100), 2, '.', ''),
        "image" => $image,
        "url" => $url,
        "title" => $title
    );
}
return $response;
}
function getPage($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($curl);
curl_close($curl);
return $html;
}
function aws_signed_request($region, $params) {
$public_key = "PUBLIC_KEY";
$private_key = "PRIVATE_KEY";
$method = "GET";
$host = "ecs.amazonaws." . $region;
$host = "webservices.amazon." . $region;
$uri = "/onca/xml";
$params["Service"] = "AWSECommerceService";
$params["AssociateTag"] = "affiliate-20"; // Put your Affiliate Code here
$params["AWSAccessKeyId"] = $public_key;
$params["Timestamp"] = gmdate("Y-m-d'TH:i:s'Z");
$params["Version"] = "2011-08-01";
ksort($params);
$canonicalized_query = array();
foreach ($params as $param => $value) {
    $param = str_replace("%7E", "~", rawurlencode($param));
    $value = str_replace("%7E", "~", rawurlencode($value));
    $canonicalized_query[] = $param . "=" . $value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method . "'n" . $host . "'n" . $uri . "'n" . $canonicalized_query;
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
$signature = str_replace("%7E", "~", rawurlencode($signature));
$request = "http://" . $host . $uri . "?" . $canonicalized_query . "&Signature=" . $signature;
$response = getPage($request);
var_dump($response);
$pxml = @simplexml_load_string($response);
if ($pxml === False) {
    return False;// no xml
} else {
    return $pxml;
}
}
?>

请尝试使用https而不是http提供规范url。

如HTTPS要求的活动内容

"活动"内容,如脚本、外部样式表和 xmlhttprequest必须通过HTTPS加载,而不是HTTP

希望这对你有用!