Php ';序列化';SimpleXMLElement';在Wordpress post_meta中保存时不允许

Php ';序列化';SimpleXMLElement';在Wordpress post_meta中保存时不允许,php,wordpress,amazon-web-services,simplexml,Php,Wordpress,Amazon Web Services,Simplexml,我正在亚马逊附属wordpress页面上工作。 为此,我使用aws_signed_request函数从amazon获取价格和链接 以下是返回xml的aws_签名_请求函数: function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag) { $method = "GET"; $host = "ecs.amazonaws.".$region; $uri

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

以下是返回xml的aws_签名_请求函数:

    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);
这会将价格添加为正确的值,但当我要添加链接时,会收到以下错误消息:

未捕获异常“exception”,消息为“的序列化” 在…中不允许使用“SimpleXMLElement”

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

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

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

问题的第二部分是SimpleXML对象是专门存储在内存中的,不能“序列化”(即转换为完全描述对象的字符串)。这意味着,如果您试图将它们保存到数据库或会话中,您将看到错误。如果您确实想保存整个XML块,可以使用
$foo->asXML()

因此,简言之:

  • 使用
    $link=(字符串)$item->DetailPageURL以获取字符串而不是对象
  • 使用
    update\u post\u meta($post->ID,'ItemXML',$item->asXML())如果要存储整个项目
可能的副本