php http build,POST请求仅在PHP中带有http_build_query

我需要使用http_build_query创建POST请求.以下是我的代码:

$uri_args = array (

'name' => 'Jack',

'surname' => 'Jackson',

'username' => 'jackson12',

'email' => 'Jack@mail.com',

);

$uri = http_build_query($uri_args);

header("Location: http://samplesite.com?$uri");

目前,它会生成类似GET的请求,但我需要POST.

考虑到我不想使用curl,…仅使用http_build_query.

解决方法:

$data = array(

'name' => 'Jack',

'surname' => 'Jackson',

'username' => 'jackson12',

'email' => 'Jack@mail.com',

);

$query = http_build_query($data);

// create context

$context = stream_context_create(array(

'http' => array(

'method' => 'POST',

'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,

'content' => $query,

),

));

// send request and collect data

$response = file_get_contents(

$target = "http://example.com/target.php",

$use_include_path = false,

$context);

//some actions with $response

//...

// redirect

header("Location: http://samplesite.com");

标签:post,http-post,php

来源: https://codeday.me/bug/20191030/1969299.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值