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. 17.3.13---socket网络套接字介绍--函数和数据类型

    1----Socket类型 套接字格式:socket(family,type[,protocal])使用给定的地址族,套接字类型,协议编号(默认为0)来创建套接字 socket类型 描述 socket ...

  2. 3)PHP基本语法和变量基础,区分大小写

    (1)语法环境: PHP嵌入到html到代码中: . <?php 这里是PHP代码 ?> 标准形式: <script language='php'> 这里是PHP代码 < ...

  3. 天融信(NAT)地址转换端口映射配置

    目的地址为公司的公网地址 服务:选择或者自己定义一个端口号,就是要映射到服务器上的那个端口号 目的地址转换为:服务器ip 目的端口转换为:选择定义的服务(端口号) 规则描述:随便写

  4. linux下 c语言调用c++

    /*****************************g++编译cpp 文件为库文件.编译C文件时gcc 要链接 -l stdc++ 这个库**(非常重要)*///定义c++ class 头文件 ...

  5. TPO4-2 Cave Art in Europe

    Perhaps, like many contemporary peoples, Upper Paleolithic men and women believed that the drawing o ...

  6. Haploid inheritance|Hardy-Weinberg proportions|

    I.2 Haploid inheritance 单倍体也有短暂的二倍体时期: Meiosis:减数分裂 依据图示信息,同时基因型A的频率是p,基因型a的频率是(1-p): 建立Hardy-Weinbe ...

  7. Tokyocabinet/Tokyotyrant文档大合集

    1. 前言 这里不是我个人原创,是我对网络上整理到的资料的再加工,以更成体系,更方便研究阅读.主要是对其中跟主题无关的文字删除,部分人称稍做修改;本人无版权,您可以将本页面视为对参考页面的镜像.第二部 ...

  8. Sqlite教程(1) SQLiteOpenHelper

    首先,创建DbHelper对象,继承SQLiteOpenHelper. Configuration是自行创建的工具类,里面都是App的一些环境设置. public class DbHelper ext ...

  9. vue中v-on支持的事件总结

    资源事件 事件名称 何时触发 error 资源加载失败时. abort 正在加载资源已经被中止时. load 资源及其相关资源已完成加载. beforeunload window,document 及 ...

  10. Django实现注册,往邮箱发送验证链接

    由于最近要做个平台,在GitHub上下载了一个系统框架,想着为了安全,实现注册时往一个邮箱发送注册信息,由管理员来确认是否同意其注册. 感谢博主:https://blog.csdn.net/geek_ ...