我試圖解析這個URL如何解析HTTPS URL重定向到另一個網址
https://graph.facebook.com/4/picture?width=378&height=378
但它重定向我到另一個鏈接:
所以,問題是我想實現代碼來使用第一個鏈接,當它重定向到第二個鏈接時,解析第二個我需要從第二個URL獲取的東西,最佳實踐是什麼?
感謝您的幫助提前
我試圖解析這個URL如何解析HTTPS URL重定向到另一個網址
https://graph.facebook.com/4/picture?width=378&height=378
但它重定向我到另一個鏈接:
所以,問題是我想實現代碼來使用第一個鏈接,當它重定向到第二個鏈接時,解析第二個我需要從第二個URL獲取的東西,最佳實踐是什麼?
感謝您的幫助提前
,捲曲:
$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;
嘗試使用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;
}
}
您可以使用此代碼:
<?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];
}
它給了我$ URL內 – Jadolyo
相同的網址我添加了'CURLOPT_SSL_VERIFYPEER'。 – Federkun