: 1. 초기화가
curl_init($ch); //initialise the curl handle
//COOKIESESSION is optional, use if you want to keep cookies in memory
curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);
단계를 곱슬 곱슬
단계 2. 헤더 가져 오기 $url
curl_setopt($ch, CURLOPT_URL, $url); //specify your URL
curl_setopt($ch, CURLOPT_HEADER, true); //include headers in http data
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); //don't follow redirects
$http_data = curl_exec($ch); //hit the $url
$curl_info = curl_getinfo($ch);
$headers = substr($http_data, 0, $curl_info['header_size']); //split out header
단계 3. 올바른 응답 코드
if (!($curl_info['http_code']>299 && $curl_info['http_code']<309)) {
//return, echo, die, whatever you like
return 'Error - http code'.curl_info['http_code'].' received.';
}
4 단계를해야합니다.새로운 URL을 얻기 위해 헤더를 구문 분석하십시오.
preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!", $headers, $matches);
$url = $matches[1];
일단 새 URL을 얻은 후에는 2-4 단계를 원하는만큼 자주 반복 할 수 있습니다.
CURLOPT_NOBODY에서 사용할 수 있습니까? – HyderA
내 프로그램은 실제로 URL이 리디렉션이 아닌 경우 본문을 사용합니다. 그래서 이것은 전혀 문제를 개선시키지 못합니다. 내 쿼리는 기본적으로 PHP 코드에서 수행하는 오버 헤드를 줄이는 Location 헤더를 추출하는 방법이 있는지 여부에 관한 것입니다. – Stewart
http://stackoverflow.com/questions/1439040/how-can-i-get-the-destination-url-using-curl –