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;
}
随机推荐
- C#利用NPOI处理excel的类 NPOIHelper.cs
个人的NPOIHelp类,包括datatable导出到excel,dataset导出到excel,excel导入到datatable,excel导入到dataset, 更新excel中的数据,验证导入 ...
- Avito Cool Challenge 2018 B. Farewell Party 【YY】
传送门:http://codeforces.com/contest/1081/problem/B B. Farewell Party time limit per test 1 second memo ...
- __future__模块
Python提供了__future__模块,把下一个新版本的特性导入到当前版本,于是我们就可以在当前版本中使用一些新版本的特性,比如除法: 在Python 2.x中,对于除法有两种情况,如果是整数相除 ...
- spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
http://blog.csdn.net/super_ccc/article/details/50728529 1.xml文件 <bean id="aaa" class=&q ...
- js去除空格(trim方法)
/** * 去空格 */ String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); ...
- ViewController里的loadView和viewDidLoad什么区别
当你访问一个ViewController的view属性时,如果此时view的值是nil,那么,ViewController就会自动调用loadView这个方法.这个方法就会加载或者创建一个view对象 ...
- vue中使用window.open会在url前自动添加本地服务器的地址bug修复
不能写成www.baidu.com 需要写成https://www.baidu.com
- LeetCode21.合并两个有序链表 JavaScript
将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...
- Java实现“睡排序”——线程池Executors的使用
前提 之前在知乎上看见一个有意思的排序算法——睡排序. 睡排序最早好像是4chan上一个用户用shell脚本实现的: 算法思想简洁明了:利用进程的sleep来实现 越大的数字越迟输出. 虽然像2L说的 ...
- JSP/Servlet开发——第十一章 Ajax交互扩展
1. jQuery实现Ajax的方法: ●除了$.ajax()方法以外,jQuery还提供了其他多种更简单的 Ajax 实现方法,如$.get().$.post().$.getJSON().对象.lo ...