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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值