PHP의 cURL을 사용하여 리다이렉트할 곳을 찾으려면 어떻게 해야 하나요?
리다이렉트를 따라 컬을 하려고 하는데 제대로 작동하지 않아요.서버에 GET 파라미터로서 송신해, 그 결과 URL 를 취득하는 문자열이 있습니다.
예제:
문자열 = Kobold Vermin
URL = www.wowhead.com/search?q=Kobold+Worker
이 URL에 접속하면, 「www.wowhead.com/npc=257」로 리다이렉트 됩니다."npc=257"을 추출하여 사용할 수 있도록 이 URL을 PHP 코드로 되돌리고 싶습니다.
현재 코드:
function npcID($name) {
$urltopost = "http://www.wowhead.com/search?q=" . $name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_URL, $urltopost);
curl_setopt($ch, CURLOPT_REFERER, "http://www.wowhead.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
단, www.wowhead.com/npc=257이 아닌 www.wowhead.com/search?q=Kobold+Worker이 반환됩니다.
외부 리다이렉트가 발생하기 전에 PHP가 반환되는 것 같습니다.어떻게 하면 고칠 수 있을까요?
cURL이 리다이렉트를 따르도록 하려면 다음 명령을 사용합니다.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
음... 난 네가 실제로 컬을 하고 있는 것 같진 않은데...시험:
curl_exec($ch);
...옵션을 설정한 후, 그리고 그 전에curl_getinfo()
불러.
편집: 페이지가 리다이렉트 되는 장소를 알고 싶다면, 여기서의 어드바이스에 따라 Curl을 사용하여 헤더를 가져와 Location: 헤더를 추출합니다.
$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);
$result = curl_exec($ch);
if (preg_match('~Location: (.*)~i', $result, $match)) {
$location = trim($match[1]);
}
이 선을 추가하여 컬 초기화
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_close 전에 getinfo를 사용합니다.
$redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
es:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 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');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$html = curl_exec($ch);
$redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
curl_close($ch);
위의 답변은 basedir와 관련된 서버 중 하나에서 사용할 수 없었기 때문에 다시 해시했습니다.아래 코드는 모든 서버에서 작동합니다.
$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);
curl_close( $ch );
// the returned headers
$headers = explode("\n",$a);
// if there is no redirection this will be the final url
$redir = $url;
// loop through the headers and check for a Location: str
$j = count($headers);
for($i = 0; $i < $j; $i++){
// if we find the Location header strip it and fill the redir var
if(strpos($headers[$i],"Location:") !== false){
$redir = trim(str_replace("Location:","",$headers[$i]));
break;
}
}
// do whatever you want with the result
echo $redir;
여기서 선택한 답은 점잖지만 대소문자를 구분하여 친척으로부터 보호하지 않습니다.location:
헤더(일부 사이트에서 사용) 또는 실제로 구문이 있을 수 있는 페이지Location:
내용상으로는...(현재 질로는 그렇게 하고 있습니다).
조금 엉성하지만, 이것을 보다 스마트하게 하기 위해 몇 가지 간단한 편집은 다음과 같습니다.
function getOriginalURL($url) {
$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);
$result = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// if it's not a redirection (3XX), move along
if ($httpStatus < 300 || $httpStatus >= 400)
return $url;
// look for a location: header to find the target URL
if(preg_match('/location: (.*)/i', $result, $r)) {
$location = trim($r[1]);
// if the location is a relative URL, attempt to make it absolute
if (preg_match('/^\/(.*)/', $location)) {
$urlParts = parse_url($url);
if ($urlParts['scheme'])
$baseURL = $urlParts['scheme'].'://';
if ($urlParts['host'])
$baseURL .= $urlParts['host'];
if ($urlParts['port'])
$baseURL .= ':'.$urlParts['port'];
return $baseURL.$location;
}
return $location;
}
return $url;
}
이 경우에도 리다이렉션은 1회밖에 진행되지 않습니다.더 자세히 알아보려면 실제로 내용을 가져와 리디렉션을 따라야 합니다.
HTTP 헤더를 취득할 필요가 있는 경우도 있습니다만, 동시에 이러한 헤더를 반환하고 싶지 않습니다.**
이 스켈레톤은 재귀를 사용하여 쿠키 및 HTTP 리다이렉트를 처리합니다.여기서의 주된 생각은 HTTP 헤더를 클라이언트코드로 되돌리지 않도록 하는 것입니다.
그 위에 매우 강한 컬 클래스를 만들 수 있습니다.POST 기능 추가 등
<?php
class curl {
static private $cookie_file = '';
static private $user_agent = '';
static private $max_redirects = 10;
static private $followlocation_allowed = true;
function __construct()
{
// set a file to store cookies
self::$cookie_file = 'cookies.txt';
// set some general User Agent
self::$user_agent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
if ( ! file_exists(self::$cookie_file) || ! is_writable(self::$cookie_file))
{
throw new Exception('Cookie file missing or not writable.');
}
// check for PHP settings that unfits
// correct functioning of CURLOPT_FOLLOWLOCATION
if (ini_get('open_basedir') != '' || ini_get('safe_mode') == 'On')
{
self::$followlocation_allowed = false;
}
}
/**
* Main method for GET requests
* @param string $url URI to get
* @return string request's body
*/
static public function get($url)
{
$process = curl_init($url);
self::_set_basic_options($process);
// this function is in charge of output request's body
// so DO NOT include HTTP headers
curl_setopt($process, CURLOPT_HEADER, 0);
if (self::$followlocation_allowed)
{
// if PHP settings allow it use AUTOMATIC REDIRECTION
curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($process, CURLOPT_MAXREDIRS, self::$max_redirects);
}
else
{
curl_setopt($process, CURLOPT_FOLLOWLOCATION, false);
}
$return = curl_exec($process);
if ($return === false)
{
throw new Exception('Curl error: ' . curl_error($process));
}
// test for redirection HTTP codes
$code = curl_getinfo($process, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302)
{
curl_close($process);
try
{
// go to extract new Location URI
$location = self::_parse_redirection_header($url);
}
catch (Exception $e)
{
throw $e;
}
// IMPORTANT return
return self::get($location);
}
curl_close($process);
return $return;
}
static function _set_basic_options($process)
{
curl_setopt($process, CURLOPT_USERAGENT, self::$user_agent);
curl_setopt($process, CURLOPT_COOKIEFILE, self::$cookie_file);
curl_setopt($process, CURLOPT_COOKIEJAR, self::$cookie_file);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($process, CURLOPT_VERBOSE, 1);
// curl_setopt($process, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
}
static function _parse_redirection_header($url)
{
$process = curl_init($url);
self::_set_basic_options($process);
// NOW we need to parse HTTP headers
curl_setopt($process, CURLOPT_HEADER, 1);
$return = curl_exec($process);
if ($return === false)
{
throw new Exception('Curl error: ' . curl_error($process));
}
curl_close($process);
if ( ! preg_match('#Location: (.*)#', $return, $location))
{
throw new Exception('No Location found');
}
if (self::$max_redirects-- <= 0)
{
throw new Exception('Max redirections reached trying to get: ' . $url);
}
return trim($location[1]);
}
}
다음을 사용할 수 있습니다.
$redirectURL = curl_getinfo($ch,CURLINFO_REDIRECT_URL);
여기 regex가 많이 있습니다.이렇게 하는 것이 더 안정적일지도 모릅니다.
$resultCurl=curl_exec($curl); //get curl result
//Optional line if you want to store the http status code
$headerHttpCode=curl_getinfo($curl,CURLINFO_HTTP_CODE);
//let's use dom and xpath
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($resultCurl, LIBXML_HTML_NODEFDTD);
libxml_use_internal_errors(false);
$xpath = new \DOMXPath($dom);
$head=$xpath->query("/html/body/p/a/@href");
$newUrl=$head[0]->nodeValue;
위치 부분은 apache에서 보낸 HTML의 링크입니다.그래서 Xpath는 그것을 회복하기에 완벽하다.
언급URL : https://stackoverflow.com/questions/3519939/how-can-i-find-where-i-will-be-redirected-using-curl-in-php
'programing' 카테고리의 다른 글
문자열을 JavaScript 함수 호출로 변환하는 방법 (0) | 2022.10.01 |
---|---|
목록 이해에서의 이중 반복 (0) | 2022.10.01 |
MySQL 5.7을 새로운 MySQL 8.0으로 업데이트하려면 어떻게 해야 합니까? (0) | 2022.10.01 |
HTTPS/SSL을 통한 Java 클라이언트 인증서 (0) | 2022.10.01 |
MySQL의 JSON 어레이를 행으로 변환 (0) | 2022.10.01 |