What alternative function can be used if allow_url_fopen is restricted?

When allow_url_fopen is restricted, an alternative function that can be used to fetch remote content is cURL. cURL is a library that allows you to connect and communicate with different types of servers with many different protocols. By using cURL, you can perform similar tasks as allow_url_fopen but with more control and security.

// Initialize cURL session
$ch = curl_init();

// Set the URL to fetch
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api/data');

// Set cURL options to handle the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the cURL session
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Output the fetched data
echo $response;