Linux 拨号vps windows公众号手机端

php怎么发送get请求头

lewis 8年前 (2017-10-29) 阅读数 9 #程序编程
文章标签 PHP

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门