php 使用file_get_contents() 发送Post请求

岳小威子
2021-09-10 / 72 个字 / 0 评论 / 50 阅读 / 加载耗时:26ms / 正在检测是否收录...
温馨提示:
本文最后更新于2021年09月10日,已超过958天没有更新,若内容或图片失效,请留言反馈。

刚好需要这个功能,就去 百度一下 找了一下,顺便记录一下,中间加了点自己的想法。

// php file_get_contents() 发送Post请求
// 方法
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;
}

// 调用
// $url #url 
// $title string
// $list array
// 最好将参数转成json然后一起抛,我试了几次,这个方法最实用
send_post($url, ['data'=>json_encode(['title'=>$title,'list'=>$list])]);

文章到这里就结束了

您在本文章已经停留了大概
喜欢的话就点个赞吧!或着请我喝个冰可乐,我就太感谢你了!


0
打赏
拜谢打赏(☆ω☆)

感谢老板,老板大气。。

评论 (0)

取消