Search code examples
phpjqueryapihyperlinkitunes

iTunes Search API Affiliate Links


I've been working with the iTunes search API to get the content from iTunes to display on my site which returns JSON data that I'm storing in the database and then displaying the data from my database. I'll be using linkshare to affiliate my links of the product to the iTunes Store. The problem I'm facing is when I save my data to the database I'm saving the iTunes URL of the product in the database (which isn't affiliated) and I'm wondering if there is away that I can integrate the linkshare affiliate link to my search API so I don't have to manually setup the affiliate link for each product every time. Thanks in advance, I appreciate the advice!

iTunes Search API URL

http://itunes.apple.com/search?term='.$term.'&limit=5&media=software&enity=software

URL RETURNED

https://itunes.apple.com/us/app/angry-birds-seasons/id398157641?mt=8&ign-mpt=uo%3D4%2522

AFFILIATE URL REQUIRED

http://click.linksynergy.com/fs-bin/stat?id=yfbyIWqHFt8&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=https%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fangry-birds-seasons%252Fid398157641%253Fmt%253D8%2526uo%253D4%2526partnerId%253D30

Solution

  • It seems like your affiliate URL equals :

    http://click.linksynergy.com/fs-bin/stat?id=yfbyIWqHFt8&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=+url component encode returned

    To make it so :

    in JS

    encodeURIComponent(
         encodeURIComponent(
             'https://itunes.apple.com/us/app/angry-birds-seasons/id398157641?mt=8&ign-mpt=uo%3D4%2522'
          )
    );
    

    would give you

    https%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fangry-birds-seasons%252Fid398157641%253Fmt%253D8%2526ign-mpt%253Duo%25253D4%25252522

    which seems to be the missing part in your affiliate URL. (without the partnerId%253D30)

    in PHP

    From https://stackoverflow.com/a/1734255/460368, you can have this function which will do the same thing as in JS :

    function encodeURIComponent($str) {
        $revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
        return strtr(rawurlencode($str), $revert);
    }
    

    So,

    echo encodeURIComponent(
             encodeURIComponent(
                 'https://itunes.apple.com/us/app/angry-birds-seasons/id398157641?mt=8&ign-mpt=uo%3D4%2522'
             )
         );
    

    will give you

    https%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fangry-birds-seasons%252Fid398157641%253Fmt%253D8%2526ign-mpt%253Duo%25253D4%25252522