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. one_day_one_linuxCmd---wget命令

    <坚持每天学习一个 linux 命令,今天我们来学习 wget 命令> Linux wget 是一个文件下载工具,它用在命令行下,使用也非常方便,不但体积小而且功能完善,并支持很多强大的功 ...

  2. i++ 和 ++i 的区别和实现

    ++i 和 i++ ++i 和 i++ 的区别 1)i++ 返回的是 i 的值,++i 返回的是 i+1 的值 2)i++ 不能用作左值,++i 可以用作左值 左值和右值的区别是什么? 根本区别是:能 ...

  3. ccf201403-3 记录一个神tmwa了的代码 莫非我没看懂题。。。

    #include <string.h> #include<cstdio> #include<stdio.h> #include <iostream> # ...

  4. tensorflow实现sphereFace网络(20层CNN)

    #coding:utf-8 import tensorflow as tf from tensorflow.python.framework import ops import numpy as np ...

  5. python如何在一个for循环中遍历多个列表

    推荐使用python内置函数zip,它可以将x个y维列表变成一个zip对象,将zip对象拆包可以发现它变成了y个x维元组.我们还可以将这个对象变成一个元组或列表.如下所示: 如果是两个列表的zip,我 ...

  6. CCPC2019网络赛

    2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A 题意:找到最小的正整数 C 使得 (A^C)&(B^C) 最小. \(A,B \le 10^9\) 签到题.这个C取 A& ...

  7. MySql 相关面试题

    1.mysql 慢查询 目的:通过慢查询日志,记录超过指定时间的 SQL 语句,优化 sql 查询 步骤:查看慢查询开启状态-->设置慢查询 http://www.cnblogs.com/luy ...

  8. Linux常见指令x-mind

  9. webservice SOA

    ------------------认证问题

  10. Centos内核更新

    内核更新操作后面补上.暂时记录删除多余内核操作 删除卸载多余内核 1.系统启动时,选择需要保留的内核进入系统,通过uname -a命令查看当前内核版本,以防误删 2. 使用rpm -qa | grep ...