当前位置: 首页 > news >正文

php post

post form

function post($remote_server,$data,$second=60){

$ch = curl_init();
if(is_string($data)){
$this_header = ["content-type: application/x-www-form-urlencoded;charset=UTF-8"];# 如果$data是字符串,则Content-Type是application/x-www-form-urlencoded

}else{
$this_header = ["content-type: multipart/form-data;charset=UTF-8"];# 如果$data是k=>v的数组,则Content-Type是multipart/form-data,
}
curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_TIMEOUT,$second);
curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false);

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$data = curl_exec($ch);
$code = curl_errno($ch);
$curl_getinfo=curl_getinfo($ch);
$http_status =$curl_getinfo['http_code'];//获取状态码
curl_close($ch);
if ($code==0&&$http_status==200){
return $data;
}elseif($code==28){
throw new Exception("超时重试");
}elseif($http_status==404){
throw new Exception("访问地址错误");
}elseif($http_status==500){
throw new Exception("内部系统错误");
}else{
throw new Exception("获取数据失败");
}

}

$url = 'http://';  //调用接口的平台服务地址

$post_string = array('a'=>'b');

post($url,$post_string);

 

post xml

    function post_xml($urn,$xmlStr){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $urn);    // 设置你准备提交的URL 
        $post_data = array(
                "content" => $xmlStr
        );
        curl_setopt($curl, CURLOPT_POST, true);  // 设置POST方式提交
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//判断是否接收返回值,0:不接收,1:接收
        $data = curl_exec($curl); // 运行curl,请求网页, 其中$data为接口返回内容
        curl_close($curl);        // 关闭curl请求
        return $data;
}

post json

 protected function post($remote_server,$post_string=null,$second=60){
        $ch = curl_init();
        $header =array("Content-type: application/json;charset=\"utf-8\"","Accept: application/json");
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_TIMEOUT,$second); 
        $data = curl_exec($ch);
        $code = curl_errno($ch);
        $curl_getinfo=curl_getinfo($ch);
        $http_status =$curl_getinfo['http_code'];//获取状态码
        curl_close($ch);
        if ($code==0&&$http_status==200){
            $headerSize = $curl_getinfo['header_size'];//得到报文头长度
            $header= substr($data, 0, $headerSize);//获取header信息
            $data= substr($data, $headerSize);//获取body信息
           if (preg_match('/ApiServerName:(.*?)\n/', $header, $result)) {
              $_SERVER['ApiServerName']=trim($result[1]);
            }
            $proxyInfo=$http_status=$headerSize=$header=$result=null;
            if(substr($data, 0,10)==='{"Code":"H'){
                $start=strpos($data, 'H');
                $end=strpos($data, '|')-$start;
                $_SERVER['ErrorCode']=substr($data,$start,$end);
            }
           return $data;
        }elseif($code==28){
           throw new AException("超时重试",203010201);
        }elseif($http_status==404){
           throw new AException("地址错误",103010202);
        }elseif($http_status==500){
           throw new AException("系统错误",103010202);
        }else{
           throw new AException("获取数据失败",103010203);
        }
        
    }

file_get_content post

$data = array ('foo' => 'bar');
$data = http_build_query($data);
$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencodedrn" .
"Content-Length: " . strlen($data) . "rn",
'content' => $data
)
);
$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);
echo $html;

sockopen 以POST方式获取完整的数据

/** 
* Socket版本 
* 使用方法: 
* $post_string = "app=socket&version=beta"; 
* request_by_socket('jb51.net','/restServer.php',$post_string); 
*/ 
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){ 
$socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout); 
if (!$socket) die("$errstr($errno)"); 
fwrite($socket,"POST $remote_path HTTP/1.0"); 
fwrite($socket,"User-Agent: Socket Example"); 
fwrite($socket,"HOST: $remote_server"); 
fwrite($socket,"Content-type: application/x-www-form-urlencoded"); 
fwrite($socket,"Content-length: ".strlen($post_string)+8.""); 
fwrite($socket,"Accept:*/*"); 
fwrite($socket,""); 
fwrite($socket,"mypost=$post_string"); 
fwrite($socket,""); 
$header = ""; 
while ($str = trim(fgets($socket,4096))) { 
$header.=$str; 
} 
$data = ""; 
while (!feof($socket)) { 
$data .= fgets($socket,4096); 
} 
return $data; 
} 

 

转载于:https://www.cnblogs.com/wangxusummer/p/4481602.html

相关文章:

  • poj--1517
  • Midas Civil的钢束生成器
  • 练习写一个工资结算系统
  • linux的各个子系统
  • 域名注冊以及域名解析设置
  • 约瑟夫环实现之非递归
  • 微信公众号-开发者-自定义菜单-CLICK事件处理
  • “我爱淘”冲刺阶段Scrum站立会议6
  • 我们还需要兼容IE样式问题吗?
  • modernizr框架之表单验证
  • jquery easyUI DataGrid 初始化的时候就显示可排序的字段
  • 从顺序查找窥探平均时间复杂度分析的一般化方法
  • 无法升级数据库....因为此版本的 SQL Server 不支持该数据库的非发布版本(539) 解决方案...
  • SSIS 2012 Error: An Integration Services class cannot be found
  • JVM 内存模型
  • [LeetCode] Wiggle Sort
  • 2017前端实习生面试总结
  • CEF与代理
  • CentOS7简单部署NFS
  • ComponentOne 2017 V2版本正式发布
  • Python爬虫--- 1.3 BS4库的解析器
  • SpringCloud集成分布式事务LCN (一)
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • Yeoman_Bower_Grunt
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 跨域
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 我有几个粽子,和一个故事
  • hi-nginx-1.3.4编译安装
  • #1014 : Trie树
  • #mysql 8.0 踩坑日记
  • (1)Nginx简介和安装教程
  • (3)llvm ir转换过程
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (八)Flask之app.route装饰器函数的参数
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (转)winform之ListView
  • ******之网络***——物理***
  • ***原理与防范
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .net6+aspose.words导出word并转pdf
  • .net反编译的九款神器
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • .NET中统一的存储过程调用方法(收藏)
  • .Net转Java自学之路—基础巩固篇十三(集合)
  • /var/spool/postfix/maildrop 下有大量文件
  • @JsonSerialize注解的使用