http://blog.163.com/fan_xy_qingyuan/blog/static/188987748201411943815244/

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;
  }
}
方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

<?php  
    function HTTP_Post($URL,$data,$cookie, $referrer="")  
    {  
        // parsing the given URL  
    $URL_Info=parse_url($URL);  
        // Building referrer  
    if($referrer=="") // if not given use this script as referrer  
    $referrer="111";  
        // making string from $data  
    foreach($data as $key=>$value)  
    $values[]="$key=".urlencode($value);  
    $data_string=implode("&",$values);  
        // Find out which port is needed - if not given use standard (=80)  
    if(!isset($URL_Info["port"]))  
    $URL_Info["port"]=80;  
        // building POST-request:  
    $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";  
    $request.="Host: ".$URL_Info["host"]."\n";  
    $request.="Referer: $referer\n";  
    $request.="Content-type: application/x-www-form-urlencoded\n";  
    $request.="Content-length: ".strlen($data_string)."\n";  
    $request.="Connection: close\n";  
        $request.="Cookie:   $cookie\n";  
        $request.="\n";  
    $request.=$data_string."\n";  
        $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);  
    fputs($fp, $request);  
    while(!feof($fp)) {  
    $result .= fgets($fp, 1024);  
    }  
    fclose($fp);  
        return $result;  
    }  
    ?> 

 

php发送post请求的4种方式的更多相关文章

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

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

  2. JavaWeb 发送post请求的2种方式(form、json)

      JavaWeb 发送post请求的2种方式(form.json) CreationTime--2018年6月20日10点15分 Author:Marydon 前提:通过HttpClient来实现 ...

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

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

  4. 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础

    1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...

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

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

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

    1.curl仍然是最好的HTTP库,没有之一. 可以解决任何复杂的应用场景中的HTTP 请求2. 文件流式的HTTP请求比较适合处理简单的HTTP POST/GET请求,但不适用于复杂的HTTP请求3 ...

  7. [转]使用 curl 发送 POST 请求的几种方式

    HTTP 的 POST 请求通常是用于提交数据,可以通过这篇文章来了解各种提交方式:四种常见的 POST 提交数据方式.做 Web 后端开发时,不可避免地要自己给自己发请求来调试接口,这里要记录的内容 ...

  8. .Net Core下发送WebRequest请求的两种方式

    1.使用RestSharp.NetCore 2.使用WebApi请求方式

  9. PHP发送POST请求的三种方式

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...

随机推荐

  1. python格式化输出的三种形式

    法一: list_a = [1, 2, 3] str_b = 'aaa' string = "There are two contents:%s, %s" % (list_a, s ...

  2. 01.Java安装及环境变量的设置

    1.下载 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.mac上安装及配 ...

  3. java添加后台缓存

    public class Cache { private String key;//缓存ID private Object value;//缓存数据 private long timeOut;//更新 ...

  4. <强化学习>开门帖

    (本系列只用作本人笔记,如果看官是以新手开始学习RL,不建议看我写的笔记昂) 今天是2020年2月7日,开始二刷david silver ulc课程.https://www.youtube.com/w ...

  5. XSL使用写法与效果

    data.xml <?xml-stylesheet type="text/xsl" href="getdata.xsl"?> <ROOT> ...

  6. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  7. 四十、LAMP与LNMP加速与缓存优化进阶实战上部

    实例: 一. 所有服务器配置定时时间同步,必须通过web server上网. 有两种方式: 1.服务器A能进行上网,作为web server ,通过指定为ntp服务器,所有服务器访问这个服务器 2.服 ...

  8. 19)PHP,数组知识

    (1)数组的基础 在PHP中,数组的下标可以是数字,也可以是字符串 在PHP中,数组元素的顺序不是由下标决定的,而是由其加入的的顺序决定 (2)数组定义: array(1,5,11,'abs',tru ...

  9. TextBox换行C#文本框换行.net文本框换行textarea换行

    在TextBox中输入的内容,显示的时候如果用lable显示,无法换行 可以使用TextBox输入,然后也使用TextBox 显示,这样换行输入的内容,显示的时候也可以换行.显示的时候可以设置一下控件 ...

  10. 蓝桥杯练习Day 2

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...