2005/1/13 木曜日

Amazonのアフィリエイト

コアな話ですが、ここに書いておくことで誰かの役に立てば。
redcruise.com/nakaokaでブログを書かれている「中岡望の目からウロコのアメリカ
に先日からアマゾンのアフィリエイト(Amazonアソシエイト)を設置しました。そのときに参考にしたページとPHPのソースです。

本家の方はものすごく見づらいです。

設置までに必要な手順。アソシエイトにまず登録し、続いてデベロッパートークンを申し込む。承認されるまでに数時間かかるようです。

以下はPHPのソース。直接HTMLを返しているので汎用性はない関数になっていますが参考になれば幸いです。意外とごくシンプルなサンプルが見当たらなかったので。

<?php
function GetAmazonData($searchword, $limit, $enc = "EUC-JP")
{
    $base = "http://webservices.amazon.co.jp/onca/xml";
    $query_string = '';
    list($usec, $sec) = explode(" ",microtime());
    srand((float)$sec + (float)$usec); // time()でも十分
    $rand = rand(1, 6); // 適当に表示される広告を散らばす

    $params = array("Service" => "AWSECommerceService",
                    "SubscriptionId" => "メールで送られてくるデベロッパートークン",
                    "AssociateTag" => "アソシエイトのID",
                    "Operation" => "ItemSearch",
                    "SortValue" => "salesrank",
                    "ResponseGroup" => "Small,Images",
                    "ItemPage" => $rand,
                    "Version" => "2004-10-04",
                    "SearchIndex" => "Books",
                    "Keywords" => $searchword,
            ); // 各オプションの説明は上記参考サイトに

    foreach ($params as $key => $value) {
        $query_string .= "$key=" . urlencode($value) . "&";
    }

    $url = "$base?$query_string";

    $buf = file_get_contents($url);
    preg_match_all("'<Item>(.*?)<DetailPageURL>(.*?)</DetailPageURL>(.*?)<SmallImage>(.*?)<URL>(
.*?)</URL>(.*?)</SmallImage>(.*?)<ItemAttributes>(.*?)<Author>(.*?)</Author>(.*?)<Title>(.*?)&l
t;/Title>(.*?)</ItemAttributes>(.*?)</Item>'s", $buf, $match);

    //  2,5,9,11
    $str = "<div class='amazonarea'>n<p class='message'>Amazon.co.jp トップセラー</p>n<table>";
    for ($i = 0; $i < $limit; $i++) {
        $str .= "<tr>";
        $str .= "<td><table><tr><td><a href='{$match[2][$i]}'><img src='{$match[5][$i]}' widt
h='41' height='60' border='0' alt=''></a></td><td valign='top'><a href='{$match[2][$i]}'>{$match[11][$i]
}</a><br><small>{$match[9][$i]}</small></td></tr></table></td>";
        $i++;
        $str .= "<td><table><tr><td><a href='{$match[2][$i]}'><img src='{$match[5][$i]}' widt
h='41' height='60' border='0' alt=''></a></td><td valign='top'><a href='{$match[2][$i]}'>{$match[11][$i]
}</a><br><small>{$match[9][$i]}</small></td></tr></table></td>";
        $str .= "</tr>";
    }
    $str .= "</table>n<p class='privacy'><a href='http://rcm.amazon.com/e/cm/privacy-policy.html?o=9' target='
_blank'>プライバシーについて</a></p></div>n";
    return mb_convert_encoding($str, $enc, "auto");
}
?>
Filed under: 雑談 — funaki @ 21:36

コメントはまだありません

コメントはまだありません。

»この投稿へのコメントの RSS フィード。

現在、コメントフォームは閉鎖中です。

Powered by WordPress, Copyright © 2024 RedCruise Corporation. All rights reserved.