読者です 読者をやめる 読者になる 読者になる

memo

memo dayo.

[php] 商品名からASIN(Amazon Standard Identification Number)を取得する

// ==============================================
function make_amazon_url ($ary_option) {
    // setting
	$access_key_id     = 'aaa';// your AWS Access Key ID
	$secret_access_key = 'bbb';// your AWS Secret Access Key
	$associate_tag     = 'ccc';// your Amazon Associate Tag
	$str_base = 'http://ecs.amazonaws.jp/onca/xml';

	// make base query
	$ary_prm = array();
	$ary_prm['Service']        = 'AWSECommerceService';
	$ary_prm['Version']        = '2009-03-31';
	$ary_prm['AWSAccessKeyId'] = $access_key_id;
	$ary_prm['AssociateTag']   = $associate_tag;
	$ary_prm['Timestamp']      = gmdate('Y-m-d\TH:i:s\Z');// ISO8601,UTC(GMT)
	foreach ($ary_option as $idx_1 => $val_1) {
		$ary_prm[$idx_1] = $val_1;
	}

	// param order by index asc
	ksort($ary_prm);

	// make canonical string
	$str_canonical = '';
	foreach ($ary_prm as $idx_1 => $val_1) {
		$str_canonical .= '&' . urlencode_rfc3986($idx_1);
		$str_canonical .= '=' . urlencode_rfc3986($val_1);
	}
	$str_canonical = substr($str_canonical, 1);
	unset($ary_prm);

	// make signature
	$ary_base = parse_url($str_base);
	$str_tosign = 'GET' . "\n" . $ary_base['host'] . "\n" . $ary_base['path'] . "\n" . $str_canonical;
	unset($ary_base);
	// hmac-sha256
	$str_signature = hash_hmac('sha256', $str_tosign, $secret_access_key, True);
	// encode base64
	$str_signature = base64_encode($str_signature);
	unset($str_tosign);

	// make request url
	$str_url = $str_base . '?' . $str_canonical . '&Signature=' . urlencode_rfc3986($str_signature);

	return $str_url;
}
// ==============================================
function title2asin ($title, $ctg='DVD') {
	$ary_aaa = array();
	$ary_aaa['Operation']   = 'ItemSearch';
	$ary_aaa['SearchIndex'] = $ctg;
	$ary_aaa['Keywords']    = $title;

	$str_xml = file_get_contents(make_amazon_url($ary_aaa));
	$obj_xml = simplexml_load_string($str_xml);
	$str_asin = '' . $obj_xml->Items->Item->ASIN;
	unset($obj_xml);
	unset($str_xml);

	return $str_asin;
}
// ==============================================
広告を非表示にする