【技术分享】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);

21楼
奺辰 2022-9-27
直接用$_POST不香吗
23楼
陌讼蜷 2022-9-27
都可以,,
1 2 3

评论

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

    95

  • 帖子

    5490

  • 关注者

    0

Copyright © 2019 凯特网.   Powered by HYBBS 2.3.4  

Runtime:0.0788s Mem:2071Kb