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

我正在尝试使用 php 创建附属链接 geenrator,我正在使用以下代码生成。但是只有当链接包含任何附属标签时,此代码才在链接的最后添加 affid。如果我使用此链接,则“ https://www.flipkart.com/whirlpool-1-5-ton-5-star-split-inverter-ac-white/p/itmf8fb8a675505d?pid=ACNFE6K2BXFY6EKX ”它会向我发送相同的信息链接而不添加任何标签。

任何人都可以帮我解决这个问题吗?

$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;


大话西游666
浏览 140回答 1
1回答

临摹微笑

试试这是否有效$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 IDif (parse_url($url, PHP_URL_QUERY)): //check if link has query string&nbsp; &nbsp; if (strpos($affstring, $url) === true) : //check if link already has affiliate ID&nbsp; &nbsp; &nbsp; &nbsp; $url = preg_replace("/(" . $affstring . ").*?(\z|&)/", "$1" . $afftag . "$2", $url);&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; $url = $url . '&' . $affstring . $afftag;&nbsp; &nbsp; endif;else:&nbsp; &nbsp; $url = $url . '?' . $affstring . $afftag;endif;解决方案#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'])):&nbsp; &nbsp; 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);
打开App,查看更多内容
随时随地看视频慕课网APP