简体   繁体   English

附属标签未在链接中正确添加

[英]Affiliate tag is not adding properly in link

I am trying to create a affiliate link geenrator using php and i am using the below code to generate.我正在尝试使用 php 创建会员链接生成器,并且我正在使用以下代码生成。 But this code add affid at last of link only when the link contains any affiliate tag.但是只有当链接包含任何附属标签时,此代码才会在链接的最后添加 affid。 If i use this link then ' https://www.flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX ' it send me the same link without adding any tag.如果我使用此链接,那么“ https://www.flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX ”会发送给我相同的链接而不添加任何标签。

Anyone can please help me reagarding this?任何人都可以帮我解决这个问题吗?

$url = "https://www.flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX";
      $afftag = 'harshk'; //our affiliate ID
        $affstring = 'affid='; // url parameter for affiliate ID
        if (parse_url($url, PHP_URL_QUERY)): //check if link has query string
            if (strpos($affstring, $url) !== true) : //check if link already has affiliate ID
                $url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $url);
        else:
                $url = $url.'&'.$affstring.$afftag;
            endif;
        else:
            $url = $url.'?'.$affstring.$afftag;
        endif;

Try if this works试试这是否有效

$url = "https://www.flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX";
$afftag = 'harshk'; //our affiliate ID
$affstring = 'affid='; // url parameter for affiliate ID
if (parse_url($url, PHP_URL_QUERY)): //check if link has query string
    if (strpos($affstring, $url) === true) : //check if link already has affiliate ID
        $url = preg_replace("/(" . $affstring . ").*?(\z|&)/", "$1" . $afftag . "$2", $url);
    else:
        $url = $url . '&' . $affstring . $afftag;
    endif;
else:
    $url = $url . '?' . $affstring . $afftag;
endif;

Solution#2解决方案#2

<?php

$urlStr = "https://www.flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX";
$afftag = 'harshk'; //our affiliate ID
$affstring = 'affid'; // url parameter for affiliate ID
$url_components = parse_url($urlStr);
$params = [];
if (isset($url_components['query'])):
    parse_str($url_components['query'], $params);
endif;
$url = "{$url_components['scheme']}://{$url_components['host']}{$url_components['path']}";
$params[$affstring] = $afftag;
$url .= "?" . http_build_query($params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM