I post a lot of products on my blog that link back to Amazon with the affiliate program. I currently use a custom plugin to format the items in a post/page. It has attributes for the target URL, tracking image src and then my own custom sizing for each list as well as my own description and product image.
The reason I have a custom plugin like this is because it's a fundamental module that I also use for other items such as posting YouTube videos and links to other posts. It makes the whole design very consistent and easy to manage.
I do not want to use an existing Amazon product plugin, because it would involve double maintenance in styling and updating the code.
I have the following code (sourced heavily from http://ift.tt/1uAg2k8) which is working as needed in my standard local development server (non-WordPress). When I place it into WordPress as a plugin to use as a shortcode, I clearly have an error because the page loads up until the point of outputting the code. If I remove all instances of querying the Amazon database, my shortcode functions as expected. WP_Debug is showing no errors either...
I have read about the problems of using cURL in WordPress plugins, so I am looking to replace that with something like WP_Http using wp_remote_get(), however I am struggling to get a working version and unsure how to use it.
Any help would be greatly appreciated!
The code:
I believe the problem lies mainly within the getPage()
function. As I mentioned, this code works perfectly fine outside of WordPress on the same localhost.
$response = getAmazonPrice("co.uk", "B00D93QR9I");
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);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$html = curl_exec($curl);
curl_close($curl);
return $html;
};
function aws_signed_request($region, $params) {
$public_key = "XXX_REMOVED"; // Public Key Here
$private_key = "XXX_REMOVED"; // Secret Key Here
$method = "GET";
$host = "ecs.amazonaws." . $region;
$host = "webservices.amazon." . $region;
$uri = "/onca/xml";
$params["Service"] = "AWSECommerceService";
$params["AssociateTag"] = "XXX_REMOVED"; // 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); // Currently not displaying anything
$pxml = @simplexml_load_string($response);
if ($pxml === False) {
return False;// no xml
} else {
return $pxml;
};
};
// END CODE FROM http://ift.tt/1uAg2k8
Aucun commentaire:
Enregistrer un commentaire