为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
大话西游666 2023-05-12 14:22:04
我正在尝试使用 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;
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

试试这是否有效


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

解决方案#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);


查看完整回答
反对 回复 2023-05-12
  • 1 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信