-1

I am trying to create a affiliate link geenrator using php. I need help to create a flipkart deep affiliate link,which can remove 'www.' and add 'dl.' if present or add 'dl.' before link. For example if input link was https://www.flipkart.com/?affid=xyz then it sends me https://dl.flipkart.com/dl/?affid=xyz . Same for the below links :-
Input link ---> Output Link
https://flipkart.com/?affid=xyz --> https://dl.flipkart.com/dl/?affid=xyz , or
https://dl.flipkart.com/?affid=xyz --> https://dl.flipkart.com/dl/?affid=xyz , or
https://dl.flipkart.com/?affid=xyz --> https://dl.flipkart.com/dl/?affid=xyz

Thanks in advance.

1 Answers1

0

Use parse_url() to get the url metadata. Now, check for host and just overwrite it with dl.flipkart.com and so goes for the path as well.

Snippet:

<?php

$tests = [
        'https://www.flipkart.com/?affid=xyz',
        'https://flipkart.com/dl?affid=xyz',
        'https://dl.flipkart.com/?affid=xyz',
        'https://dl.flipkart.com/?affid=xyz',
        'https://dl.flipkart.com/dl?affid=xyz',
        'https://flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX'
    ];
    
foreach($tests as $test){
    echo $test," => ",getNewURL($test),PHP_EOL;
}

function getNewURL($url){
    $url_parts = parse_url($url);
    $url_parts['host'] = 'dl.flipkart.com';
    $url_parts['path'] .= "/";
    if(strpos($url_parts['path'],"/dl/") !== 0) $url_parts['path'] = '/dl/'.trim($url_parts['path'],"/");

    return $url_parts['scheme'] . "://" . $url_parts['host'] . $url_parts['path'] . (empty($url_parts['query']) ? '' : '?' . $url_parts['query']); 
}
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • There was a issue when i give 'https://flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX' instead of 'https://dl.flipkart.com/dl/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX' i am getting 'https://dl.flipkart.com/dl?pid=ACNFE6K2BXFY6EKX'. – Harsh Kumar Jun 27 '20 at 15:57
  • There is issue which i found in the code if the link is https://flipkart.com/?affid=xyz this give output as https://dl.flipkart.com/dl?affid=xyz instead of https://dl.flipkart.com/dl/?affid=xyz . And this make the link won't work. only '/' this is missing after '/dl' in url path. – Harsh Kumar Jul 16 '20 at 16:55
  • @HarshKumar what difference will it make? – nice_dev Jul 16 '20 at 18:35
  • When i open this in mobile phones it says invalid/broken link. – Harsh Kumar Jul 17 '20 at 02:48
  • @HarshKumar There should be some issue with .htaccess if you are using Apache server. – nice_dev Jul 17 '20 at 04:51
  • I had checked but nothing problem is there. You can try opening this link in phone https://dl.flipkart.com/dl?affid=xyz it will say link is broken. I know am bothering you sorry for that. – Harsh Kumar Jul 17 '20 at 05:21
  • @HarshKumar Don't worry, you aren't bothering me. If it works on desktop but not on mobile is very unlikely for a problem to exist with the link itself. – nice_dev Jul 17 '20 at 06:02
  • Flipkart uses deeplink to track shopping in mobile devices so it need links to be like https://dl.flipkart.com/dl/ which on opening it redirect to https://www.flipkart.com/ and in the above link only '/dl' in present so it can't able to redirect it to https://www.flipkart.com/. And that's why this problem is happening. – Harsh Kumar Jul 17 '20 at 06:15
  • @HarshKumar I still think the issue might be with .htaccess. Anyways, I have updated my code. – nice_dev Jul 17 '20 at 06:48
  • 1
    I have check .htaccess it has nothing only one code of no-index. Anyway thanks for your help. I am so glad that you helped me. – Harsh Kumar Jul 17 '20 at 07:12
  • @HarshKumar I have updated my answer anyway. If this is not from .htaccess, it might be something to do with internal routing because `/dl` is a perfectly valid request URI. – nice_dev Jul 17 '20 at 08:06
  • 1
    Thanks . I have modified this code little one and now it’s working fine. Thanks for all your help! – Harsh Kumar Jul 18 '20 at 04:56