温馨提示×

php怎么发送get请求头

PHP
小亿
109
2023-11-09 10:35:44
栏目: 编程语言

在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请求,并传递请求上下文作为第三个参数。

0