我工作的亚马逊子公司WordPress的页面上。 对于我现在用的是aws_signed_request函数来获取从亚马逊的价格和纽带。
这里是aws_signed_request函数返回的XML:
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;
}
}
从那以后,我得到职位的阿辛和生成的链接和价格
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.";
}
当我回声$链接和$价格这一切工作不错。 但我想保存在WordPress后的自定义字段中的值,所以我不必每次运行的功能。
update_post_meta($post->ID, 'Price', $price);
update_post_meta($post->ID, 'Link', $link);
这增加了价格为正确的值,但是当我想要添加的链接我得到这个错误信息:
未捕获的异常“异常”与消息的SimpleXMLElement“的序列化'是不允许的”在...
但是,当我删除$ parsed_xml = ...功能,这样可以节省空值。