cafe24 file_get_contents 함수를 대체할 소스를 찾고있습니다.
본문
로컬에선 file_get_contents()를 이용해서 youtube정보를 가져오는걸 만들었습니다.
그런데 cafe24 호스팅에선 사용하지 못한다고 하고 allow_url_fopen 은 보안이 위험해진다는 글을 보고
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$g = curl_exec($ch);
curl_close($ch);
return $g;
}
function get_url_fsockopen( $url ) {
$URL_parsed = parse_url($url);
$host = $URL_parsed["host"];
$port = $URL_parsed["port"];
if ($port==0)
$port = 80;
$path = $URL_parsed["path"];
if ($URL_parsed["query"] != "")
$path .= "?".$URL_parsed["query"];
$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
return "$errstr ($errno)<br>\n";
} else {
fputs($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 128);
if ( $body )
$in .= $s;
if ( $s == "\r\n" )
$body = true;
}
fclose($fp);
return $in;
}
}
두 함수를 사용했는데 curl을 사용한건 빈값을 주고
fsock을 사용한건 $body = false; 를 임시로 $body = true; 주고 실행하면
이렇게 줍니다..
그래서 질문드리는건 제가 올린 두 함수에서 잘못사용하고 있는곳이나,
다른 함수를 알려주시면 감사합니다. (꾸벅)
!-->!-->
답변 2
다른 웹 사이트도 요청을 보내보시고 결과값이 어떤지 비교해보세요.
유튜브는 정책상 막혀있을 확률이 높습니다.
유튜브 정보는 유튜브 api를 발급받아 요청하여 사용하는것이 편합니다
유튜브의 경우 무분별한 크롤링이나 외부접근을 막기 위해 허용된 api를 제외한 방법은 막혀있는걸로 알고 있습니다.
string(490) "HTTP/1.1 301 Moved Permanently Strict-Transport-Security: max-age=31536000; includeSubDomains; preload Location: https://www.youtube.com/get_video_info?video_id=MW_kYAFav-c Content-Length: 0 Date: Fri, 05 Feb 2021 12:48:15 GMT Content-Type: text/html Server: YouTube Frontend Proxy X-XSS-Protection: 0 Alt-Svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43" "
호스트를 www.youtube.com 으로!!
https://www.youtube.com/get_video_info?video_id=MW_kYAFav-c
curl에서 리턴받은 http_code가 301/302/307/308 등 컨텐츠가 moved 되었으면
response header에서 가리키는 location url 을 읽어서 curl 함수를 재귀로 호출...