在 php 中发送 post 请求-ag捕鱼王app官网

在 php 中发送 post 请求

作者:迹忆客 最近更新:2023/03/29 浏览次数:

我们将介绍一种使用无 curl 的方法通过 http_build_query()stream_context_create()file_get_contents() 函数在 php 中发送 post 请求的方法。

我们将向你展示一种使用 curl 在 php 中发送 post 请求的方法。curl 用于将 http 请求发送到 web 服务器。curl 模块通常预装 php。如果尚未安装,我们应该手动安装它,以便使用 php 发送服务器的请求。

我们将向你展示一个示例,该示例通过创建一个函数来使用 curl 使用 php 发送 post 请求。此方法与第二种方法非常相似。它使用用户定义的函数来发送请求。我们可以重复使用提供不同发布数据和 url 的代码。


在 php 中使用 stream_context_create()file_get_contents() 函数的无 curl 方法发送 post 请求

我们可以使用 http_build_query()stream_context_create()file_get_contents() 之类的函数在 php 中发送 post 请求,而无需使用 curl。我们可以使用 http_build_query() 函数创建查询参数以发送 post 请求。我们可以创建一个数组来指定 http 标头,方法和内容。我们使用 stream_context_create() 函数来处理流数据。file_get_contents() 函数将 url 的内容读取为字符串。我们使用 $_post 全局变量读取数据。

例如,创建 http_build_query() 函数并在函数内部创建一个数组。在数组中分别创建键 nameid 以及值 robert1。为函数分配变量 $postdata。创建一个数组以指定 http 方法,标头和内容。将键值 methodheadercontent 设置为 postcontent-type: application/x-www-form-urlencoded 和变量 $postdata。将此数组包装在另一个数组 http 中。将数组分配给变量 $opts。将 $opts 作为 stream_context_create() 函数的参数,并将其分配给变量 $context。使用 file_get_contents() 函数并将 url http://localhost/request.php 用作第一个参数。使用布尔值 false 和变量 $context 作为第二和第三个参数。将函数分配给变量 $result 并打印。创建一个 php 文件 request.php 并使用 $_post 变量回显 nameid 键。

在下面的示例中,nameid 是使用 post 方法发布的数据。request.php 文件使用 $_post 变量读取查询参数。

示例代码:

# php 7.*
php
$postdata = http_build_query(
    array(
        'name' => 'robert',
        'id' => '1'
    )
);
$opts = array('http' =>
    array(
        'method' => 'post',
        'header' => 'content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context = stream_context_create($opts);
$result = file_get_contents('http://localhost/request.php', false, $context);
echo $result;
?>
# php 7.*
php
echo "name: ". $_post['name']. "
"
;
echo "id: ". $_post['id']; ?>

输出:

name: robert
id: 1

在 php 中使用 curl 来发送 post 请求

curl 代表客户端 url。我们可以使用 curl 通过 php 脚本发送 post 请求。php 版本需要 curl 模块才能执行不同的 curl 函数。在这种方法中,我们像上面的方法一样使用 http_build_query() 函数作为查询参数。我们可以使用 curl_init() 函数在脚本中初始化 curl 连接。我们将 curl_setopt() 函数与 curlopt_urlcurlopt_postcurlopt_postfields 等选项一起使用。这些选项分别设置 url,请求 http post 请求和 post 数据。我们可以使用 curl_setopt() 函数返回 url 的内容,并使用 curl_exec() 函数执行 post 请求。

例如,在数组 $fields 中创建键 nameid 以及值 wayne2。将数组用作 http_build_query() 函数中的参数,并为该函数分配变量 $postdata。使用 curl_init() 函数打开一个 curl 连接,并将其分配给变量 $ch。编写三个 curl_setopt() 函数,并将 $ch 变量用作所有三个函数的第一个参数。作为函数中的第二个参数,分别在第一个、第二个和第三个函数中写入选项 curlopt_urlcurlopt_postcurlopt_postfields。在这三个函数中,将第三个参数分别设置为 http://localhost/request.phptrue$postdata。调用函数 curl_setopt() 并使用 $ch 变量作为第一个参数,使用 curlopt_returntransfer 选项作为第二个参数,并使用布尔值 true 作为第三个参数。从 $result 变量调用 curl_exec($ch) 函数并打印 $result。使用与第一种方法相同的 request.php 文件。

示例代码:

#php 7.x
php
$fields = [
    'name' => 'wayne',
    'id' => 2,
];
$postdata = http_build_query($fields);
$ch = curl_init()
curl_setopt($ch,curlopt_url, 'http://localhost/request.php');
curl_setopt($ch,curlopt_post, true);
curl_setopt($ch,curlopt_postfields, $postdata);
curl_setopt($ch,curlopt_returntransfer, true);
$result = curl_exec($ch);
echo $result;
?>

输出:

name: wayne 
id: 2

在 php 中创建一个用户定义的函数,该函数使用 curl 发送 post 请求

我们可以创建一个方法来获取 url 输入和 post 数据以发送 post 请求。此方法还使用 curl 作为第二种方法。它像以前一样使用所有 curl 功能。此方法旨在将代码重用于多个 url 和数据。

例如,创建一个函数 httppost() 并将变量 $url$data 用作参数。使用 curl_init() 函数打开 curl 连接,并以变量 $url 作为参数。与第二种方法一样,使用三个 curl_setopt() 函数。使用 curl_exec() 方法执行 post 数据。创建数组 $data 并创建键 nameid 以及值 scott3。以 http://localhost/request.php 作为 url,以 $data 数组作为数据来调用 httppost() 方法。此方法还使用 request.php 文件作为上述两种方法。

代码示例:

#php 7.x
php
function httppost($url, $data){
    $curl = curl_init($url);
    curl_setopt($curl, curlopt_post, true);
    curl_setopt($curl, curlopt_postfields, http_build_query($data));
    curl_setopt($curl, curlopt_returntransfer, true);
    $response = curl_exec($curl);
    echo $response;
}
$data = [
    'name' => 'scott',
    'id' => 3
];
httppost('http://localhost/request.php', $data);
?>

输出:

name: scott 
id: 3

上一篇:

下一篇:在 php 中防止 sql 注入

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 php 中获取时间差的分钟数

发布时间:2023/03/29 浏览次数:204 分类:php

本文介绍了如何在 php 中获取时间差的分钟数,包括 date_diff()函数和数学公式。它包括 date_diff()函数和数学公式。

发布时间:2023/03/29 浏览次数:156 分类:php

本教程演示了如何将用户从页面重定向到 php 中的其他页面

php 分页

发布时间:2023/03/29 浏览次数:95 分类:php

本教程介绍如何在 php 中对数据库行进行分页

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图