博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php如何发post请求
阅读量:7232 次
发布时间:2019-06-29

本文共 1520 字,大约阅读时间需要 5 分钟。

/**     *  post提交数据     * @param  string $url 请求Url     * @param  array $datas 提交的数据     * @return url响应返回的html     */    function sendPost($url, $datas) {        $temps = array();        foreach ($datas as $key => $value) {            $temps[] = sprintf('%s=%s', $key, $value);        }        $post_data = implode('&', $temps);        $url_info = parse_url($url);        if(empty($url_info['port']))        {            $url_info['port']=80;        }        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";         //get请求,这里就改为get        $httpheader.= "Host:" . $url_info['host'] . "\r\n";        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";       //描述HTTP消息实体的传输长度        $httpheader.= "Connection:close\r\n\r\n";        $httpheader.= $post_data;        $fd = fsockopen($url_info['host'], $url_info['port']);          //post请求        fwrite($fd, $httpheader);        $gets = "";        $headerFlag = true;        while (!feof($fd)) {                          //检测是否到达了文件末尾,如果服务器没有关闭fsockopen,feof会等到超时   默认的超时限制是 60 秒,可以使用 stream_set_timeout() 来改变这个值。            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {                break;            }        }        while (!feof($fd)) {            $gets.= fread($fd, 128);     //如果只是想将一个文件的内容读入到一个字符串中,请使用 ,它的性能比 fread() 好得多        }        fclose($fd);        return $gets;    }

亲测好用

转载于:https://www.cnblogs.com/hanshuai0921/p/7928975.html

你可能感兴趣的文章
neural_transfer风格迁移
查看>>
测试ip
查看>>
ex1.1
查看>>
day10_friest_自动化
查看>>
Entity Framework直接执行SQL语句
查看>>
NodeJS Socket Broadcast
查看>>
BZOJ5168:[HAOI2014]贴海报(线段树)
查看>>
<%@Page%>中的Codebehind AytoEventWireup.inherits有何作用?
查看>>
64. Minimum Path Sum
查看>>
SQL Server 导入bak备份出错
查看>>
JavaScript中的私有/静态属性
查看>>
Ubuntu下安装XAMPP
查看>>
C# ExpandoObject用法
查看>>
【SICP练习】135 练习3.66
查看>>
数据挖掘——文本挖掘-关键字提取
查看>>
Codeforces Gym - 101102A - Coins
查看>>
webstorm识别 ftl文件
查看>>
在Window 下安装Redis数据库
查看>>
主席树 | | 可持久化线段树
查看>>
JSTL中c:set标签的要点和技巧
查看>>