下面介绍 PHP 中常用的 HTTP 请求方式及其主要参数。
1. file_get_contents
file_get_contents
是一个简单的函数,用于从文件或 URL 读取数据。在发送 HTTP 请求时,这个函数可以用于 GET 请求。
示例代码:
$response = file_get_contents($url, $use_include_path, $context, $offset, $maxlen);
参数详解:
-
$url
(string):要读取的文件或 URL 的路径。在发送 HTTP 请求时,这里填写目标 URL。 -
$use_include_path
(bool, 可选):是否在 include_path(包含路径)中查找文件。默认值为false
,通常在发送 HTTP 请求时不需要更改。 -
$context
(resource, 可选):这是一个上下文资源,创建它以定制流的行为。对于 HTTP 请求,可以使用stream_context_create
函数创建一个上下文,用于设置 HTTP 请求头、请求方法等。 -
$offset
(int, 可选):开始读取的位置。默认从文件的开头开始读取。在发送 HTTP 请求时,通常不需要设置这个参数。 -
$maxlen
(int, 可选):读取的最大长度。如果指定了,读取的数据不会超过这个长度。在发送 HTTP 请求时通常不设置这个参数。
示例:
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'User-Agent: PHP'
]
]);
$response = file_get_contents("https://example.com/api", false, $context);
2. cURL
cURL
是一个更强大的 HTTP 请求工具,适合需要处理复杂请求的场景。以下是 curl_setopt
函数常用的参数。
示例代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
参数详解:
-
curl_setopt($ch, CURLOPT_URL, $url)
:设置请求的 URL。 -
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $bool)
:true
:返回请求的结果而不是直接输出。这通常用于存储响应数据。false
:直接输出请求结果(默认行为)。
-
curl_setopt($ch, CURLOPT_POST, $bool)
:指定请求方法是否为 POST。true
:发送 POST 请求。false
:发送 GET 请求(默认)。
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data)
:用于发送 POST 请求时传递数据。可以是 URL 编码后的字符串或数组。 -
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers)
:设置 HTTP 请求头。$headers
是一个包含多个请求头的数组。 -
curl_setopt($ch, CURLOPT_TIMEOUT, $seconds)
:设置请求超时时间,单位是秒。 -
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $bool)
:true
:验证对方的 SSL 证书。false
:不验证对方的 SSL 证书(不推荐,除非测试环境下)。
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $bool)
:true
:允许cURL
自动跟随重定向。false
:禁止跟随重定向。
示例:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['key' => 'value']));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$response = curl_exec($ch);
curl_close($ch);
3. Guzzle
Guzzle
是一个强大的 PHP HTTP 客户端库,支持更复杂的 HTTP 请求和响应处理。使用前需要通过 Composer 安装。
示例代码:
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://example.com/api');
参数详解:
-
new \GuzzleHttp\Client($config)
:$config
(array, 可选):配置数组,包含默认请求头、超时时间、代理设置等。
-
$client->request($method, $uri, $options)
:$method
(string):HTTP 请求方法,如GET
、POST
、PUT
、DELETE
等。$uri
(string):目标 URI 或 URL。$options
(array, 可选):请求选项数组,包括:headers
:设置 HTTP 请求头。query
:GET 请求参数,自动添加到 URL 中。form_params
:POST 请求参数,作为application/x-www-form-urlencoded
提交。json
:POST 请求参数,作为 JSON 提交,自动设置 Content-Type 为application/json
。timeout
:设置请求超时时间(秒)。http_errors
:是否抛出 HTTP 错误异常,true
为抛出(默认),false
为不抛出。
示例:
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com',
'timeout' => 2.0,
]);
$response = $client->request('POST', '/api', [
'form_params' => [
'key1' => 'value1',
'key2' => 'value2',
],
'headers' => [
'User-Agent' => 'MyApp/1.0',
'Accept' => 'application/json',
],
]);
echo $response->getBody();
总结
以上三种方法各有优劣:
file_get_contents
:适合简单的 GET 请求,方便快捷,但功能较少。cURL
:功能强大,适用于复杂的 HTTP 请求,且支持多种选项。Guzzle
:现代化的 HTTP 客户端,支持异步、并发等高级特性,适合大型项目。