What are some ways to connect to a URL in PHP without directly opening it?

When you want to connect to a URL in PHP without directly opening it in a browser, you can use functions like cURL or file_get_contents. These functions allow you to retrieve the content of a URL programmatically without the need for a user to open it in a browser. This can be useful for tasks like web scraping, API requests, or fetching remote data.

// Using cURL to connect to a URL in PHP
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

// Using file_get_contents to connect to a URL in PHP
$url = "http://example.com";
$output = file_get_contents($url);

// Output the content of the URL
echo $output;