php发送post请求,koa2接受数据

简介: php发送post请求,koa2接受数据

test.php

<?php
function send_post($url, $post_data) {
    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间(单位:s)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
$post_data = array(
    'order_id' => '1445',
    'order_sn' => '142251244555'
);
//koa接收post接口,需要加上http://
$url = 'http://localhost:3000/orderinfo';

$re = send_post($url,$post_data);
echo ($re);

app.js

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');

// 等于const router = require('koa-router')();
const Router = require('koa-router');
const router = new Router();
const app = new Koa();


// log request URL:
app.use(async (ctx, next) => {
    console.log(`${ctx.request.method} ${ctx.request.url}`); // 打印URL
    await next(); // 调用下一个middleware
});

// add get-url-route:
router.get('/orderinfo/:id', async (ctx, next) => {
    var
        order_id = ctx.params.order_id,
        order_sn = ctx.params.order_sn;
    console.log(`${order_id},${order_sn}`);
    ctx.response.body = `<h1>Hello, ${order_id},${order_sn}!</h1>`;
});

router.get('/', async (ctx, next) => {
    ctx.response.body = '<h1>this is qioku fabric API!</h1>';
});

router.get('/order', async (ctx, next) => {
    ctx.response.body = `
        <form action="/orderinfo" method="post">
            <p>order_id: <input name="order_id" value=""></p>
            <p>order_sn: <input name="order_sn" value=""></p>
            <p><input type="submit" value="Submit"></p>
        </form>`;
});

// add post-url-route:
router.post('/orderinfo', async (ctx, next) => {
    var
        order_id = ctx.request.body.order_id || '',
        order_sn = ctx.request.body.order_sn || '';
    //console.log(ctx.request);
    console.log(`${order_id},${order_sn}`);
    // 返回
    ctx.response.body = '<h1>这是第'+`${order_id}`+'份订单,订单号为'+`${order_sn}`+'!</h1>';
});

/*app.use(async (ctx, next) => {
    const start = new Date().getTime(); // 当前时间
    await next(); // 调用下一个middleware
    const ms = new Date().getTime() - start; // 耗费时间
    console.log(`Time: ${ms}ms`); // 打印耗费时间
});*/

// 对于任何请求,app将调用该异步函数处理请求:
// ctx是koa封装了request和response的变量
// next是koa传入的将要处理的下一个异步函数
app.use(async (ctx, next) => {
    await next();
    ctx.response.type = 'text/html';
    ctx.response.body += '<h1>Hello, koa2!</h1>';
});

// 必须在router之前
app.use(bodyParser());
// add router middleware:
app.use(router.routes());

// 在端口3000监听:
app.listen(3000);
console.log('app started at port 3000...');

结果:在这里插入图片描述

form.html也可以接受,注意http://

<form action="http://localhost:3000/orderinfo" method="post">
    <p>order_id: <input name="order_id" value=""></p>
    <p>order_sn: <input name="order_sn" value=""></p>
    <p><input type="submit" value="Submit"></p>
</form>
目录
相关文章
|
6月前
|
JSON 数据处理 PHP
PHP数组处理技巧:高效操作数据集合
PHP数组处理技巧:高效操作数据集合
|
6月前
|
JSON 安全 大数据
PHP中的数组处理艺术:灵活高效的数据操作
PHP中的数组处理艺术:灵活高效的数据操作
|
6月前
|
JSON 定位技术 PHP
PHP技巧:解析JSON及提取数据
这就是在PHP世界里探索JSON数据的艺术。这场狩猎不仅仅是为了获得数据,而是一种透彻理解数据结构的行动,让数据在你的编码海洋中畅游。通过这次冒险,你已经掌握了打开数据宝箱的钥匙。紧握它,让你在编程世界中随心所欲地航行。
229 67
|
9月前
|
数据库连接 PHP 数据库
【YashanDB知识库】PHP使用ODBC驱动无法获取长度为256char以上的数据
【YashanDB知识库】PHP使用ODBC驱动无法获取长度为256char以上的数据
|
9月前
|
Oracle 关系型数据库 MySQL
【YashanDB知识库】php查询超过256长度字符串,数据被截断的问题
本文分析了YashanDB中PHP通过ODBC查询数据时出现的数据截断问题,表现为超过256字节的数据被截断,以及isql工具无法显示超过300字节长度的数据。问题根源在于YashanDB的ODBC驱动仅支持单次查询,且PHP扩展库默认缓冲区限制。解决方案包括改用PHP ODBC扩展库而非PDO_ODBC,以及调整isql代码逻辑以支持循环取数或一次性读取完整数据。文章还提供了具体代码示例和规避方法,适用于23.2.4.14及更早版本。
【YashanDB知识库】php查询超过256长度字符串,数据被截断的问题
|
3月前
|
关系型数据库 MySQL PHP
PHP和Mysql前后端交互效果实现
本文介绍了使用PHP连接MySQL数据库的基本函数及其实现案例。内容涵盖数据库连接、选择数据库、执行查询、获取结果等常用操作,并通过用户登录和修改密码的功能实例,展示了PHP与MySQL的交互过程及代码实现。
300 0
PHP和Mysql前后端交互效果实现
|
8月前
|
关系型数据库 MySQL Linux
查看Linux、Apache、MySQL、PHP版本的技巧
以上就是查看Linux、Apache、MySQL、PHP版本信息的方法。希望这些信息能帮助你更好地理解和使用你的LAMP技术栈。
412 17
|
前端开发 关系型数据库 MySQL
PHP与MySQL动态网站开发实战指南####
【10月更文挑战第21天】 本文将深入浅出地探讨如何使用PHP与MySQL构建一个动态网站,从环境搭建到项目部署,全程实战演示。无论你是编程新手还是希望巩固Web开发技能的老手,都能在这篇文章中找到实用的技巧和启发。我们将一起探索如何通过PHP处理用户请求,利用MySQL存储数据,并最终呈现动态内容给用户,打造属于自己的在线平台。 ####
544 0
|
9月前
|
关系型数据库 MySQL PHP
源码编译安装LAMP(HTTP服务,MYSQL ,PHP,以及bbs论坛)
通过以上步骤,你可以成功地在一台Linux服务器上从源码编译并安装LAMP环境,并配置一个BBS论坛(Discuz!)。这些步骤涵盖了从安装依赖、下载源代码、配置编译到安装完成的所有细节。每个命令的解释确保了过程的透明度,使即使是非专业人士也能够理解整个流程。
260 18
|
10月前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
404 25