检查Sharesale是否链接目标URL

时间:2017-06-13 09:21:38

标签: php url-redirection http-redirect

我使用Shareasale affiliate API在我的数据库中导入商家产品。我以这种格式获取产品Url:

http://www.shareasale.com/m-pr.cfm?merchantID=XXXXX&userID=YYYY&productID=ZZZZ

我正在尝试查找上面链接重定向的目标网址。如果我们在Chrome浏览器中检查网络工具,它将首先视为200,然后是301。

没有301和302重定向检查工具能够识别正确的URL。我试过这些工具: http://www.redirect-checker.org/ http://www.seoreviewtools.com/redirect-checker-tool/

我正在寻找可以找到目标网址的PHP脚本。我已经尝试了几个脚本,比如下面的代码,但是它没有用。

$urls = array(
'http://www.apple.com/imac',
'http://www.shareasale.com/m-pr.cfm?
 merchantID=xxxxx&userID=YYYY&productID=zzzzzz'
 );

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

foreach($urls as $url) {
 curl_setopt($ch, CURLOPT_URL, $url);
 $out = curl_exec($ch);

// line endings is the wonkiest piece of this whole thing
$out = str_replace("\r", "", $out);

// only look at the headers
$headers_end = strpos($out, "\n\n");
if( $headers_end !== false ) { 
    $out = substr($out, 0, $headers_end);
}   

$headers = explode("\n", $out);
foreach($headers as $header) {
    if( substr($header, 0, 10) == "Location: " ) { 
        $target = substr($header, 10);

        echo "[$url] redirects to [$target]<br>";
        continue 2;
    }   
}   

 echo "[$url] does not redirect<br>";
}

它给了我以下结果:

 [http://www.apple.com/imac] redirects to [https://www.apple.com/imac]
 [http://www.shareasale.com/m-pr.cfm?merchantID=31933&userID=897279&productID=502320302] does not redirect

请建议我解决。

0 个答案:

没有答案