2013-07-04 49 views
8

Tôi có liên kết này:php lấy url của chuyển hướng từ url nguồn

http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm

Nếu bạn truy cập nó, điều này trở thành:

http://www.liberoquotidiano.it/news/1273567/I-saggi-per-le-riforme-costituzionali-Chiaccherano-e-ascoltano.html

Làm thế nào tôi có thể nhận được các liên kết thứ hai từ đầu tiên?

Tôi đã cố gắng này, nhưng không có tác dụng, trở về tôi liên kết đầu tiên cùng:

<?php 
$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$a = curl_exec($ch); 
print_r($a);echo"<br>"; 
if(preg_match('#Location: (.*)#', $a, $r)){ 
$l = trim($r[1]); 
echo $l; 
}else echo "not working"; 
?> 

Thanks a lot.

+0

Rõ ràng trong mã? 'curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true);' – HamZa

+1

Tôi không hiểu – michele

+0

Xin lỗi, tôi đã kiểm tra mã nguồn. Trình duyệt được chuyển hướng bằng javascript. Vì vậy, bạn phải đọc mã html/js, phân tích cú pháp, nhận url và sau đó lấy nội dung từ url đó ... – HamZa

Trả lời

2

Bạn có thể nhận URL cuối cùng của yêu cầu với curl_getinfo.

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_exec($ch); 
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
+0

điều này không thể được sử dụng trong ví dụ này, nhấp chuột đầu tiên của liên kết chỉ chứa quảng cáo và một liên kết đến liên kết thứ hai. nó không phải là chuyển hướng tiêu đề. Nó chỉ trở thành một chuyển hướng tiêu đề khi người dùng đã nhấp vào nó một lần và có một cookie, tôi giả định ... – rorypicko

+0

LRI: 'PHP Cảnh báo: curl_setopt() hy vọng tham số 1 là tài nguyên, chuỗi ' –

28

Nhờ @king-isaac mã sau đã được thử nghiệm và hoạt động.

<?php 

$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm"; 
$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); 

$a = 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 

// Uncomment to see all headers 
/* 
echo "<pre>"; 
print_r($a);echo"<br>"; 
echo "</pre>"; 
*/ 

echo $url; // Voila 
?> 
+0

Giải pháp tốt, bạn có thể thêm hỗ trợ không cokies? xem [redirator DOI] (http://doi.org/10.1016/S1473-3099 (16) 30318-8), trả về http://secure.jbs.elsevierhealth.com/action/cookieAbsent –