共计 1464 个字符,预计需要花费 4 分钟才能阅读完成。
- 使用原生的 PHP 函数发送 GET 请求:
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$url = 'http://example.com/api?' . http_build_query($data);
$response = file_get_contents($url);
- 使用原生的 PHP 函数发送 POST 请求:
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents('http://example.com/api', false, $context);
- 使用 cURL 库发送 GET 请求:
$ch = curl_init();
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$url = 'http://example.com/api?' . http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
- 使用 cURL 库发送 POST 请求:
$ch = curl_init();
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
- 使用 Guzzle 库发送 GET 请求:
$client = new GuzzleHttp\Client();
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$response = $client->get('http://example.com/api', ['query' => $data]);
$body = $response->getBody();
- 使用 Guzzle 库发送 POST 请求:
$client = new GuzzleHttp\Client();
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$response = $client->post('http://example.com/api', ['form_params' => $data]);
$body = $response->getBody();
丸趣 TV 网 – 提供最优质的资源集合!
正文完