gpt4 book ai didi

php - '在 Wordpress post_meta 中保存时不允许序列化 'SimpleXMLElement'

转载 作者:IT王子 更新时间:2023-10-29 00:16:17 26 4
gpt4 key购买 nike

我正在处理亚马逊附属 wordpress 页面。为此,我使用 aws_signed_request 函数从亚马逊获取价格和链接。

这是返回 xml 的 aws_signed_request 函数:

    function  aws_signed_request($region, $params, $public_key, $private_key, $associate_tag) {
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";

$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
$params["AssociateTag"] = $associate_tag;
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
$params["Version"] = "2009-03-31";

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;

/* calculate the signature using HMAC, SHA256 and base64-encoding */
$signature = base64_encode(hash_hmac("sha256",
$string_to_sign, $private_key, True));

/* encode the signature for the request */
$signature = str_replace("%7E", "~", rawurlencode($signature));

/* create request */
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;

/* I prefer using CURL */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$xml_response = curl_exec($ch);

if ($xml_response === False)
{
return False;
}
else
{
$parsed_xml = @simplexml_load_string($xml_response);
return ($parsed_xml === False) ? False : $parsed_xml;
}
}

之后我从帖子中获取 asin 并生成链接和价格

global $post;  
$asin = get_post_meta($post->ID, 'ASIN', true);

$public_key = 'xxxxxxxxxxx';
$private_key = 'xxxxxxxxxxx';
$associate_tag = 'xxxxxxxxxxx';

$xml = aws_signed_Request('de',
array(
"MerchantId"=>"Amazon",
"Operation"=>"ItemLookup",
"ItemId"=>$asin,
"ResponseGroup"=>"Medium, Offers"),
$public_key,$private_key,$associate_tag);

$item = $xml->Items->Item;

$link = $item->DetailPageURL;
$price_amount = $item->OfferSummary->LowestNewPrice->Amount;
if ($price_amount > 0) {
$price_rund = $price_amount/100;
$price = number_format($price_rund, 2, ',', '.');
} else {
$price= "n.v.";
}

当我回显 $link 和 $price 时,这一切都很好。但我想将值保存在 wordpress 帖子的自定义字段中,这样我就不必每次都运行该函数。

update_post_meta($post->ID, 'Price', $price);
update_post_meta($post->ID, 'Link', $link);

这会将价格添加为正确的值,但是当我想添加链接时,我收到此错误消息:

Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in...

但是当我删除 $parsed_xml=... 函数时,它会保存一个空值。

最佳答案

(几乎)当您遍历一个 SimpleXML 对象时返回的所有内容实际上都是另一个 SimpleXML 对象。这就是让您编写 $item->OfferSummary->LowestNewPrice->Amount 的原因:在 $item 对象上请求 ->OfferSummary 返回一个表示 OfferSummary XML 节点的对象,因此您可以在该对象上请求 ->LowestNewPrice,等等。请注意,这也适用于属性 - $someNode['someAttribute'] 将是一个对象,而不是字符串!

为了获得元素或属性的字符串内容,您必须使用语法 (string)$variable 来“转换”它。有时,PHP 会知道您打算这样做,并为您做这件事 - 例如在使用 echo 时 - 但一般来说,始终手动转换为字符串是一种很好的做法这样您以后更改代码时就不会感到意外。您还可以使用 (int) 转换为整数,或使用 (float) 转换为 float 。

问题的第二部分是 SimpleXML 对象在内存中的存储相当特殊,不能“序列化”(即变成一个完整描述对象的字符串)。这意味着如果您尝试将它们保存到数据库或 session 中,您将得到您所看到的错误。如果您真的想保存整个 XML block ,可以使用 $foo->asXML()

所以,简而言之:

  • 使用 $link = (string)$item->DetailPageURL; 获取字符串而不是对象
  • 如果您想存储整个项目,请使用 update_post_meta($post->ID, 'ItemXML', $item->asXML());

关于php - '在 Wordpress post_meta 中保存时不允许序列化 'SimpleXMLElement',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14912551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com