What are the differences between sending a GET request and a POST request in PHP?

When sending a GET request in PHP, the data is sent in the URL as key-value pairs, which can be seen in the browser's address bar. On the other hand, when sending a POST request, the data is sent in the request body, which is not visible in the URL. GET requests are generally used for retrieving data, while POST requests are used for submitting data securely.

// Example of sending a GET request in PHP
$response = file_get_contents('http://example.com/api/data?key1=value1&key2=value2');

// Example of sending a POST request in PHP
$data = array('key1' => 'value1', 'key2' => 'value2');
$options = array(
    'http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$response = file_get_contents('http://example.com/api/data', false, $context);