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>
目录
相关文章
|
4月前
|
前端开发 PHP
【PHP学习】—get请求传递参数(五)
【PHP学习】—get请求传递参数(五)
|
7月前
|
Ubuntu PHP Windows
在安装PHP时出现了冲突请求的问题
在安装PHP时出现了冲突请求的问题
83 1
|
JSON 前端开发 PHP
在php里发送请求,php发送http请求的几种方法
还在等什么,快来一起讨论关注吧,公众号【八点半技术站】,欢迎加入社群
|
8月前
|
JSON PHP 数据格式
PHP - Laravel 接口请求返回 JSON 数据
PHP - Laravel 接口请求返回 JSON 数据
150 0
|
9月前
|
前端开发 PHP
php解决ajax使用post请求时提交的数据过多而导致et::ERR_CONNECTION_RESET的解决方案
php解决ajax使用post请求时提交的数据过多而导致et::ERR_CONNECTION_RESET的解决方案
131 0
|
9月前
|
PHP
PHP开发中$_GET请求转为$_POST获取参数的解决方案
PHP开发中$_GET请求转为$_POST获取参数的解决方案
39 0
|
9月前
|
JSON PHP 数据格式
PHP中json传递请求字符串网址函数http_build_query()与parse_str(),将POST参数组转换拼接成GET请求链接
PHP中json传递请求字符串网址函数http_build_query()与parse_str(),将POST参数组转换拼接成GET请求链接
96 0
|
安全 前端开发 中间件
PHP:laravel中间件和控制器的请求参数传递与获取
PHP:laravel中间件和控制器的请求参数传递与获取
184 1
PHP:laravel中间件和控制器的请求参数传递与获取
|
存储 缓存 分布式计算
如何使用PHP处理大规模的并发请求?底层原理是什么?
如何使用PHP处理大规模的并发请求?底层原理是什么?
|
网络协议 PHP
【解决方案】PHP使用CURL请求时,遇到name lookup timed out 如何解决
【解决方案】PHP使用CURL请求时,遇到name lookup timed out 如何解决
260 0