如何从php中的rstyle链接获取完整的URL

时间:2017-05-10 17:00:01

标签: php redirect hyperlink

我有一个链接:http://rstyle.me/n/bfj42hqgww

重定向到https://www.missguided.co.uk/clothing/coats-jackets/double-breasted-tailored-long-faux-wool-coat-camel

如何获取重定向链接?

我尝试了以下内容:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
echo $url;

但它不起作用。我也尝试过:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // We want to just get redirect url but not to follow it.
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
echo !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';

2 个答案:

答案 0 :(得分:0)

我对此没有任何了解,但我在我的网站上使用类似于发送短信的内容。所以,如果是锻炼,你可以试试。

$URL="example.com";  
$ch = curl_init($URL);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$output = curl_exec($ch);      
curl_close($ch); 

答案 1 :(得分:0)

你没有从服务器重定向,而是从javascript。 cURL不会执行javascript。当我打开你的链接时,我看到真正的链接隐藏在"标题"标签

$url = 'http://rstyle.me/n/bfj42hqgww';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

$data = curl_exec($curl);
curl_close($curl);

// Get real URL.
preg_match("|<title>(.+?)</title>|si", $data, $matches);

$link = ( isset($matches[1]) ) ? $matches[1] : '';

echo $link;