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

沙发
小骨头 2022-8-29
不戳[真棒]
板凳
陌讼蜷 2022-8-29
必须的[滑稽][玫瑰]
地板
上梁歪 2022-8-29
看不懂,但我还是大为震撼[滑稽]
4楼
陌讼蜷 2022-8-29
哈哈哈[滑稽]
5楼
Elegy 2022-8-29
我想问一下我用get请求一个api是乱码全部乱码不只是中文 都是那种菱形和问号 在浏览器打开就没事,这个怎么解决呢
6楼
陌讼蜷 2022-8-29
浏览器打开截图我看看[玫瑰]
7楼
Elegy 2022-8-29
不好意思啊电脑已经关了准备睡觉了 可以发手机上的浏览器嘛
8楼
陌讼蜷 2022-8-29
可以的[玫瑰]
9楼
Elegy 2022-8-29
第一张是直接在浏览器访问api第二个是用php 请求的
我是php小白 今天搞了一天没搞明白

10楼
陌讼蜷 2022-8-29
接口发我我看看

评论

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

    95

  • 帖子

    5490

  • 关注者

    0

Copyright © 2019 凯特网.   Powered by HYBBS 2.3.4  

Runtime:0.0988s Mem:2135Kb