Cyrozap's Tech Projects

Computers. Circuits. Code.

PHP Script to Convert Normal Links Into Amazon Affiliate Links

I like to give my friends and family recommendations on products. I like to give them Amazon Affiliate links. The problem? Whenever I want to do that, there's an annoying process I have to go to to do that, and only I can use that process. If anyone wants to buy something and they want to credit me, they can't make affiliate links and have to ask me to make a link for them. So, to remedy this, I took 2 hours out of my day to make a PHP script that anyone can use to make Affiliate links from normal Amazon product links. How does it work? Well, it's quite simple, really. The user enters their Amazon product link, then my script takes that link, strips the product ID from it, and adds that product ID to my pre-formatted link, which is then displayed to the user to click on. It's under a CC-BY-SA 3.0 License the Apache License 2.0, so feel free to distribute (and modify) it as long as you credit me for the original.

Here it is:

<head>
    <title>Cyrozap&#039;s Amazon Affiliate Link Generator</title>
</head>
<h2>Enter the Amazon link you want to convert, then click the "Make Affiliate Link" button.</h2>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">
    <p>
        <input type="text" name="link">
        <input type="submit" value="Make Affiliate Link!">
    </p>
</form>
<?php
$affiliate = "?ie=UTF8&tag=cyrstecpro-20&linkCode=as2&camp=1789&creative=390957&creativeASIN="; // This is what is in all of my Amazon Affiliate links. To get yours, make an affiliate link, then look for where it has a "?" then copy all the characters from the "?" to the "=" including those two signs.
$link = htmlspecialchars($_GET['link']); // This is the original Amazon link that is entered by the user.
if (isset($_GET['link'])) {
    $pid = substr(strstr($link,"p/"),2,10);
    echo "<h4>Here's your new Amazon Affiliate link: </h4>";
    //echo "http://www.amazon.com/gp/product/", $pid, $affiliate, $pid; // Uncomment this line to just make a text link.
    echo "<h4><a href=http://www.amazon.com/gp/product/", $pid, $affiliate, $pid, ">http://www.amazon.com/gp/product/", $pid, $affiliate, $pid, "</a></h4>"; // This line makes a "clickable" link.
}
?>
<br>
<p>
    License: <a rel="license" href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>
    <br>Original script by <a href="http://cyrozap.com/">cyrozap</a>
</p>

Comments