// urlencode() 사용
$str = 'Hello, wold!';
$encoded_str = urlencode($str);
echo $encoded_str; // 출력: Hello%2C+wold%21
echo '<br>';

// rawurlencode() 사용
$str = 'Hello, wold!';
$encoded_str = rawurlencode($str);
echo $encoded_str; // 출력: Hello%2C%20wold%21
echo '<br>';

// http_build_query() 사용
$array = [
    'name' => 'hypertext processor',
    'age' => 30,
];

$encoded_query = http_build_query($array, '', '&', PHP_QUERY_RFC1738);
echo $encoded_query; // 출력: name=hypertext+processor&age=30
echo '<br>';

$encoded_query = http_build_query($array, '', '&', PHP_QUERY_RFC3986);
echo $encoded_query; // 출력: name=hypertext%20processor&age=30
$userinput = 'Data123!@-_ +';
echo "UserInput: $userinput\n";
echo '<a href="mycgi?foo=' . urlencode($userinput) . '">';

/* 출력:
UserInput: Data123!@-_ +
<a href="mycgi?foo=Data123%21%40-_+%2B">
*/
$server_url = 'https://www.example.com';
$image_path = '/images/my image.jpg';

// 파일 경로를 URL에 추가할 때 rawurlencode() 함수 사용
$encoded_image_path = rawurlencode($image_path);

$image_url = $server_url . $encoded_image_path;

echo $image_url;

// 출력: https://www.example.com%2Fimages%2Fmy%20image.jpg
$api_endpoint = 'https://api.example.com/data';
$api_key = 'your_api_key';
$params = array(
    'api_key' => $api_key,
    'action' => 'get_data',
    'format' => 'json'
);

$api_url = $api_endpoint . '?' . http_build_query($params);

// $api_url은 "https://api.example.com/data?api_key=your_api_key&action=get_data&format=json"와 같은 형태가 됩니다.