php 通过多种方法访问 url:file_get_contents():获取 url 内容并将其存储在变量中。curl:使用 curl 库进行更复杂的 http 请求。fopen() 和 stream_get_contents():使用流函数来访问 url。guzzlehttp/client:使用 guzzlehttp 库来简化 http 请求。
PHP 中访问 URL 的方法
PHP 提供了多种方法来访问 URL,包括:
1. file_get_contents()
$url = 'https://example.com';
$content = file_get_contents($url);
2. curl
立即学习“PHP免费学习笔记(深入)”;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
3. fopen() 和 stream_get_contents()
$handle = fopen($url, 'r');
$content = stream_get_contents($handle);
fclose($handle);
4. GuzzleHTTP/Client
use GuzzleHttpClient;
$client = new Client();
$response = $client->get($url);
$content = $response->getBody()->getContents();