class Request{

    public static function post($url, $post_data = '', $timeout = 5){//curl

        $ch = curl_init();

        curl_setopt ($ch, CURLOPT_URL, $url);

        curl_setopt ($ch, CURLOPT_POST, 1);

        if($post_data != ''){

            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

        }

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        curl_setopt($ch, CURLOPT_HEADER, false);

        $file_contents = curl_exec($ch);

        curl_close($ch);

        return $file_contents;

    }

    public static function post2($url, $data){//file_get_content

        $postdata = http_build_query(

            $data

        );

        $opts = array('http' =>

                      array(

                          'method'  => 'POST',

                          'header'  => 'Content-type: application/x-www-form-urlencoded',

                          'content' => $postdata

                      )

        );

        $context = stream_context_create($opts);

        $result = file_get_contents($url, false, $context);

        return $result;

    }

    public static function post3($host,$path,$query,$others=''){//fsocket

        $post="POST $path HTTP/1.1\r\nHost: $host\r\n";

        $post.="Content-type: application/x-www-form-";

        $post.="urlencoded\r\n${others}";

        $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";

        $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";

        $h=fsockopen($host,80);

        fwrite($h,$post);

        for($a=0,$r='';!$a;){

                $b=fread($h,8192);

                $r.=$b;

                $a=(($b=='')?1:0);

            }

        fclose($h);

        return $r;

    }
}

PHP发送POST请求的三种方式的更多相关文章

  1. 使用jmeter发送put请求的三种方式

    之前在前公司使用jmeter调试接口时,由于都是get和post请求,所以一直是顺风顺水的,毫无阻拦在短时间内调试完所有接口. 但是呢,在换到新公司后,发现接口请求是多式多样的,get.post必须有 ...

  2. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  3. RocketMQ(6)---发送普通消息(三种方式)

    发送普通消息(三种方式) RocketMQ 发送普通消息有三种实现方式:可靠同步发送.可靠异步发送.单向(Oneway)发送. 注意 :顺序消息只支持可靠同步发送. GitHub地址: https:/ ...

  4. php发送post请求的三种方法示例

    本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...

  5. java实现HTTP请求的三种方式

    目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...

  6. 【转载】java实现HTTP请求的三种方式

    目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...

  7. HTTP:Java实现HTTP请求的三种方式

    目前JAVA实现HTTP请求的方法用的最多的有两种: 一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,H ...

  8. PHP发送HTTP请求的几种方式

    转发:https://blog.tanteng.me/2017/07/php-curl-guzzlehttp/ 1)PHP开发中我们常用CURL 方式封装 HTTP请求,什么是CURL? CURL 是 ...

  9. django-Ajax发送POST请求(csrf跨站请求的三种方式),文件的上传

    第一种 <script> $(".eq").on("click",function () { $.ajax({ url:"/eq/&quo ...

随机推荐

  1. Linux学习计划

    用Linux已经有很长一段时间了,先后用过Debian和CentOS,但一直没有系统地去学习,现在一是有时间,二是工作中需要用到,所以准备用心去学习一下. 一.教材 1.主要以<鸟哥Linux& ...

  2. Fragment 事务 回退栈

    一些相关API 1.Fragment常用的三个类: android.app.Fragment 定义android.app.FragmentManager 用于在Activity中操作Fragmenta ...

  3. 外网访问自己的tomcat

    我们平常学习时经常会写一下javaweb程序,我们为了更能逼近现实,就想着自己的javaweb程序发布后,外网的同学能够访问我们的网站,难道我们去买空间,去买域名嘛,其实也没必要,我们只是学习,测试之 ...

  4. java里面的equals和hashcode的总结

    问题1: java比较两个对象,除了equals,为什么还要重写hashcode方法? 基本类型比较,用==就可以了. 对象比较,equals比较是对象的内存地址,hashcode比较的也是对象的内存 ...

  5. Examples_08_03

    访问本地程序.http://192.168.1.103/preg_match/test.php,如果换成localhost或者127.0.0.1,则会导致无法访问. http://blog.csdn. ...

  6. (转)ASP.NET里面简单的记住用户名和密码

    using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using S ...

  7. String or binary data would be truncated

    在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...

  8. 【转】windows消息16进制对应表

    来源:http://blog.sina.com.cn/s/blog_962250db0101d4mj.html windows mobile编程,无论使用eVC还是.net CF,都脱不开window ...

  9. 武汉科技大学ACM :1004: 华科版C语言程序设计教程(第二版)课后习题3.7

    Problem Description 输入无符号短整数k[hex.]和p[oct.],将k的高字节作为结果的低字节,p的高字节作为结果的高字节组成一个新的整数. Input k[hex.]和p[oc ...

  10. class类名的管理

    var a=document.querySelector(".div1"); a.classList.remove("div2");//减掉一个 a.class ...