PHP 发送 POST 请求的示例
以下是一个使用 PHP 发送 POST 请求的示例代码:
$url = 'https://example.com/api';
$data = array('username' => 'user123', 'password' => 'pass456');
$options = array(
'http' => array(
'header' => 'Content-type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
在上面的代码中,我们首先定义了要发送请求的 URL 和要发送的数据。然后,我们使用 stream_context_create 函数创建一个上下文,包含请求的选项。最后,我们使用 file_get_contents 函数发送请求并获取响应。
注意: 在实际情况中,您可能需要使用 curl 或其他 HTTP 客户端库来发送 POST 请求,以便更好地控制请求和处理响应。
原文地址: http://www.cveoy.top/t/topic/l46c 著作权归作者所有。请勿转载和采集!