To connect to a URL in PHP, I typically use cURL. First, I initialize a cURL session with curl_init(), set the URL with curl_setopt(), and then execute it using curl_exec(). Finally, I close the session with curl_close(). This method is flexible and allows for handling various HTTP requests.
Here’s a quick example:
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'http://example.com');
- $response = curl_exec($ch);
- curl_close($ch);