2015-10-25 78 views
1

我試圖解析這個URL如何解析HTTPS URL重定向到另一個網址

https://graph.facebook.com/4/picture?width=378&height=378

但它重定向我到另一個鏈接:

https://z-n.ak.fbcdn.net/profile.ak/hprofile-ak-xfp1/v/t1.0-1/p480x480/10390028_10102210419817761_5871103530921178170_n.jpg?oh=9d44ae6370a6481a6e4e5c42d7850e2a&oe=56C84426&gda=1455827889_efa267e73eea3dd27aa02526c323a1e6

所以,問題是我想實現代碼來使用第一個鏈接,當它重定向到第二個鏈接時,解析第二個我需要從第二個URL獲取的東西,最佳實踐是什麼?

感謝您的幫助提前

回答

2

,捲曲:

$url = 'https://graph.facebook.com/4/picture?width=378&height=378'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirects 
curl_setopt($ch, CURLOPT_HEADER, false); // no needs to pass the headers to the data stream 
curl_setopt($ch, CURLOPT_NOBODY, true); // get the resource without a body 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // accept any server certificate 
curl_exec($ch); 

// get the last used URL 
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 

curl_close($ch); 

echo $lastUrl; 
+0

它給了我$ URL內 – Jadolyo

+0

相同的網址我添加了'CURLOPT_SSL_VERIFYPEER'。 – Federkun

0

嘗試使用get_headers功能

<? 
    $link = 'https://graph.facebook.com/4/picture?width=378&height=378'; 
    $headers = get_headers($link); 
    foreach ($headers as $header) { 
     if (preg_match('/^Location:\s(.*)/', $header, $out)) { 
      echo $out[1]; 
      break; 
     } 
    } 
+0

這個人給我這個錯誤信息「Warning:g et_headers():SSL操作失敗,代碼爲1. OpenSSL錯誤消息:錯誤:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:證書驗證失敗D:\ xampp \ htdocs \ facebook-profile-picture-scraper \ test.php on line 3「 – Jadolyo

+0

對於https,您需要捲曲。 – fico7489

1

您可以使用此代碼:

<?php 
$url = "https://graph.facebook.com/4/picture?width=378&height=378"; 
$content = get_url_data($url); 
$code = $content['code']; 
$headers_array = $content['headers_array']; 
$redirect_url = ''; 

if($code == 301 || $code == 302){ 
    $headers = $content['headers_array']; 
    if(isset($headers['Location'])){ 
     $redirect_url = $headers['Location'][0]; 
    } 
} 
echo 'Redirected url:' . $redirect_url; 



function get_url_data($url, $timeout = 5){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,   $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"); 

    $response = curl_exec($ch); 
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
    $header = substr($response, 0, $header_size); 
    $body = substr($response, $header_size); 
    $headers = explode("\n", $header); 

    $code = 0; 
    if(isset($headers[0])){ 
     if(preg_match('/[0-9]{3}/', $headers[0], $matches)){ 
      $code = $matches[0]; 
     } 
    } 

    $headers_array = []; 
    foreach($headers as $h){ 
     $index = strpos($h, ":"); 
     if($index !== false){ 
      $key = trim(substr($h, 0, $index)); 
      $value = trim(substr($h, ($index+1))); 
      $headers_array[$key] = [$value]; 
     } 
    } 

    return ['headers' => $headers, 'body' => $body, 'code' => $code, 'headers_array' => $headers_array]; 
} 
+0

它沒有給我任何結果與https – Jadolyo

+0

噢我的,它不工作,因爲https。現在我添加了「curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE);」現在你可以試試。 – fico7489

+0

其實這工作正常與您的網址,只與HTTP工作?或者它不適用於子域? – Jadolyo

相關問題