添加深度 links 到 flipkart 联盟 link

Add deep links to flipkart affiliate link

我正在尝试使用 php 创建附属 link 生成器。我需要帮助来创建一个 flipkart 深层会员 link,它可以删除“www.”并添加“dl.”(如果存在)或者在 link 之前添加 'dl.'。例如,如果输入 link 是 https://www.flipkart.com/?affid=xyz then it sends me https://dl.flipkart.com/dl/?affid=xyz 。以下 links 相同:-
输入 link ---> 输出 Link
https://flipkart.com/?affid=xyz --> https://dl.flipkart.com/dl/?affid=xyz
https://dl.flipkart.com/?affid=xyz --> https://dl.flipkart.com/dl/?affid=xyz
https://dl.flipkart.com/?affid=xyz --> https://dl.flipkart.com/dl/?affid=xyz

提前致谢。

使用 parse_url() 获取 url 元数据。现在,检查主机并用 dl.flipkart.com 覆盖它,路径也是如此。

片段:

<?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']); 
}