PHP file_get_contents, https 오류

DongHyun Kim·2022년 5월 17일
0

이 글은 PHP의 file_get_contents 함수로 https로 작성된 url을 열다가 마주친 오류들을 해결한 과정을 적었습니다.

우선 https는 http + SSL(Secure Socket Layer)로 http의 보안을 강화시킨 버전으로, 서버에서부터 브라우저로 전송되는 정보가 암호화되지 않았던 문제를 보완시킨 것입니다. 또한 구글 크롬에선 거의 전 웹사이트를 https로 사용하길 권장하는 추세입니다.

그래서 이를 서버에서 사용하려면 암호화를 하는 기능이 필요하기 때문에 openSSL을 작동시켜야 합니다. (이미 wamp를 설치하면서 포함되어있음)

해결 과정 중 마주친 오류들

Warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

Failed to open stream: No such file or directory

Fatal error: Uncaught Error: Call to undefined function curl_init()

문제였던 코드

Https 문제

<?php
$url = "https://www.php.net/manual/en/function.file-get-contents.php";
$info = file_get_contents($url);
echo $info;
?>

Curl 문제

function curl_get_contents($url)
{
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  $data = curl_exec($curl);
  curl_close($curl);
  return $data;
}
echo curl_get_contents($url);

< 해결 과정 >

php.ini 파일 내용 수정!

(php.ini 파일은 Binami/wampstack-버전명/php 아래에 있는 파일을 수정)

;extension_dir = "ext" -> 주석(";") 제거 -> ext 폴더 (저의 경우는 php 폴더 아래에 같이 들어있었음)의 절대경로로 수정
e.g. extension_dir = "C:\Bitnami\wampstack-8.1.5-0\php\ext"

allow_url_fopen = On

allow_url_include = On

;extension=curl -> 주석(";") 제거 -> extension=curl

;extension=opens -> 마찬가지로 주석 제거

폴더에 파일 있는지 확인!

ext폴더 아래에 php_curl.dll, php_openssl.dll 있는지 확인

php폴더에 libssh2.dll 그리고 동일한 파일이 apache2폴더 밑에 bin폴더에 있는지 확인 없다면 복사 붙여넣기

이 과정 중 extension_dir를 상대경로로 지정해서 실행해봤는데 계속 파일을 못 찾아서 절대경로로 수정 후 정상 작동했습니다.

curl, ssl 모두 정상 작동한다면

phpinfo()를 출력해보면 curl 항목이 생기고 enabled 상태로 되어있어야합니다.

profile
do programming yourself

0개의 댓글