
file_get_contents 함수에서 URL 인코딩을 자동으로 처리하는 방법은 없습니다. 하지만, URL 인코딩을 직접 처리할 수 있습니다.
1. url_encode 함수 사용하기
* url_encode 함수를 사용하여 키워드를 인코딩한 후 URL에 추가할 수 있습니다.
#hostingforum.kr
php
$url = "https://example.com/search?q=" . urlencode("키워드");
2. http_build_query 함수 사용하기
* http_build_query 함수를 사용하여 키워드를 인코딩한 후 URL에 추가할 수 있습니다.
#hostingforum.kr
php
$query = array("q" => "키워드");
$url = "https://example.com/search?" . http_build_query($query);
3. curl 함수 사용하기
* curl 함수를 사용하여 URL에 GET 요청을 보내고, 특수문자를 인코딩할 수 있습니다.
#hostingforum.kr
php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/search?q=키워드");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
4. file_get_contents 함수에 인코딩된 URL 전달하기
* 특수문자를 인코딩한 URL을 직접 작성하여 file_get_contents 함수에 전달할 수 있습니다.
#hostingforum.kr
php
$url = "https://example.com/search?q=" . urlencode("키워드");
$data = json_decode(file_get_contents($url), true);
이러한 방법 중 하나를 사용하여 URL 인코딩을 처리할 수 있습니다.
2025-07-05 12:43