I am working on an amazon affiliate wordpress page. For that I am using the aws_signed_request function to get the price and link from amazon.
Here is the aws_signed_request function returning the 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;
}
}
After that I get the asin from the post and generate the link and price
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.";
}
This all works pretty good when I echo the $link and $price. But I want to save the values in the custom fields of the wordpress post so I don't have to run the function every time.
update_post_meta($post->ID, 'Price', $price);
update_post_meta($post->ID, 'Link', $link);
This adds the price as the correct value, but when I want to add the link I get this error message:
Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in...
But when I remove the $parsed_xml=... function, it saves an empty value.