php怎么发送get请求头
在PHP中,你可以使用file_get_contents()
函数来发送GET请求,并设置请求头。以下是一个示例代码:
<?php
// 创建请求头数组
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n"
)
);
// 创建请求上下文
$context = stream_context_create($options);
// 发送GET请求
$response = file_get_contents('http://example.com/api', false, $context);
// 输出响应内容
echo $response;
?>
在上述示例代码中,我们首先创建了一个包含Content-Type
请求头的关联数组$options
。然后使用stream_context_create()
函数将该数组转换为请求上下文。最后,通过调用file_get_contents()
函数发送GET请求,并传递请求上下文作为第三个参数。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:个人服务器部署流程是什么 下一篇:python中set的用法是什么
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。