Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中将此封面url解析为最终目标url_Php_Url_Curl - Fatal编程技术网

在php中将此封面url解析为最终目标url

在php中将此封面url解析为最终目标url,php,url,curl,Php,Url,Curl,我需要将广告URL($siteURL)重定向到该URL通常在浏览器中重定向的最终目的地。假设它在example.com上重定向,我需要example.com作为输出 $siteURL="http://clkuk.tradedoubler.com/click...."; $ch = curl_init($siteURL); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETUR

我需要将广告URL($siteURL)重定向到该URL通常在浏览器中重定向的最终目的地。假设它在example.com上重定向,我需要example.com作为输出

$siteURL="http://clkuk.tradedoubler.com/click....";

    $ch = curl_init($siteURL);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_Setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    $response = curl_exec($ch);

    $info = curl_getinfo($ch);
    //print_r($info);
    echo $info['url'];
当前输出:$siteURL值(无重定向)


预期输出:如果它在example.com上重定向,我需要example.com作为输出。

使用php的parse_url,你应该得到你需要的,看看他们的doxuement:

$siteURL="http://clkuk.tradedoubler.com/click....";

    $ch = curl_init($siteURL);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_Setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    $response = curl_exec($ch);

    $info = curl_getinfo($ch);
    //print_r($info);
    echo $info['url'];
编辑: 很抱歉您需要使用parse_str而不是parse_url,请参见下面的示例:

$siteURL="http://scripts.affiliatefuture.com/AFClick.asp?affiliateID=125272&merchantID=6441&programmeID=17908&mediaID=0&tracking=74299X1524119Xa4d1bde1feb3809c50f9f4d03e2f1096&url=https%3A%2F%2Fwww.poshbingo.co.uk%2F";

parse_str($siteURL, $output);
var_dump($output['url']);

在您的情况下,重定向不是在服务器端使用
位置:
标题(后面是
cURL
)完成的,而是通过
HTML标记在浏览器中完成的

$siteURL
的正文包含以下内容:

。。。
...
cURL
不遵循HTML/JavaScript重定向,只有浏览器遵循,因此您需要实际解析
$siteURL
的主体并手动提取链接

test.php编辑:处理HTTP头和HTML/JS重定向)


这只是将url解析为数组(3){[“主机”]=>string(15)“www.example.com”[“路径”]=>string(5)”/path“[“查询”]=>string(17)“googleguy=googley”}等部分。我需要找到最终的目标url,其中该$siteURL对$siteURL=”“不起作用;当我们在浏览器中正常打开时,它会转到自动移动url,但您的代码显示错误我也这么想,但我无法使用reg exp进行元刷新:DBBUT bro$siteURL=“”:(这是因为第二个链接使用头重定向服务器端。我已经编辑了代码来处理这两种情况。thxs bro您的指南和演示代码意味着很多,您可以指导我如何处理此链接$siteURL=“”;此情况不起作用$URL=array(“,”);
<?php

$urls = array (
    "http://some.domain.test/AFClick.asp?...",
    "http://another.domain.example/click?..."
);

foreach ($urls as $siteURL) {
    $ch = curl_init($siteURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);

    print("\nOriginal URL: ${siteURL}\n");
    if (empty($info['redirect_url'])) {
        preg_match('/<meta http-equiv="refresh".*url=(http.*)">/', $response, $matches);
        print("Redirects to: ${matches[1]}\n");
    } else {
        print("Redirects to: ${info['redirect_url']}\n");
    }

    curl_close($ch);
}

?>