我正在尝试解析此网址
https://graph.facebook.com/4/picture?width=378&height=378
但它会将我重定向到另一个链接:
所以问题是我想实现代码以使用第一个链接,当它重定向到第二个链接时,解析第二个我需要从第二个URL获取内容,最佳做法是什么?
提前感谢您的帮助
答案 0 :(得分:2)
使用CURL:
$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;
答案 1 :(得分: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];
}
答案 2 :(得分: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;
}
}