【技术分享】php发送post请求的方法(一)

编程开发   © 文章版权由 admin 解释,禁止匿名转载

#楼主# 2022-8-29

方法一:file_get_content

/**

* 发送post请求

* @param string $url 请求地址

* @param array $post_data post键值对数据

* @return string

*/

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(

'username' => 'stclair2201',

'password' => 'handan'

);

send_post('http://localhost', $post_data);

11楼
陌讼蜷 2022-8-29
应该是方式的问题
12楼
Elegy 2022-8-29
可以发一个get请求的方式嘛
13楼
陌讼蜷 2022-8-29
前面发过噢
14楼
陌讼蜷 2022-8-29
你翻我以前的帖子吧
15楼
Elegy 2022-8-29
好的谢谢 我去翻翻你的帖子
16楼
陌讼蜷 2022-8-29
嗯嗯[玫瑰]
可以直接发一个封装的函数方法
18楼
总监ฅ 2022-8-29
@header('Content-Type: text/html; charset=UTF-8');[滑稽]设置一下,php文件开头
19楼
Elegy 2022-8-29
试过了的,还是一样
20楼
奺辰 2022-9-27
好麻烦啊[吐舌]

评论

登录后才可发表内容
  • 主题

    95

  • 帖子

    5490

  • 关注者

    0

Copyright © 2019 凯特网.   Powered by HYBBS 2.3.4  

Runtime:0.0525s Mem:2132Kb