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;
}
随机推荐
- HTML DOM 初学笔记
JavaScript HTML DOM 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树,如图: 通过可编程的对象 ...
- iOS 人机交互指导方针(iOS Human Interface Guidelines)
iOS 人机交互指导方针(iOS Human Interface Guidelines) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名 ...
- javascript中的回调函数(callback) (转载)
代码如下: app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(er ...
- 微信小程序开发工具快捷键
格式调整 //保存文件 Ctrl+S //代码行缩进 Ctrl+[, Ctrl+] //折叠打开代码块 Ctrl+Shift+[, Ctrl+Shift+] //复制粘贴,如果没有选中任何文字则复制粘 ...
- js去除空格(trim方法)
/** * 去空格 */ String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); ...
- javascript返回顶部插件+源码
javascript插件->returnTop.js: /* ** 插件名称returnTop.js ** 调用返回头部单例参数说明 ** 调用方式:turn.init(ele,speed); ...
- 在ASP.NET Core中怎么使用HttpContext.Current (转载)
一.前言 我们都知道,ASP.NET Core作为最新的框架,在MVC5和ASP.NET WebForm的基础上做了大量的重构.如果我们想使用以前版本中的HttpContext.Current的话,目 ...
- CSS 高级选择器
相信各位如我一样的“抠图崽”和前端大佬们,在写网页样式的实话,总是免不了写下各种各样的选择器,再给选择的元素写入样式.最基本的元素选择器.class选择器.ID选择器等就不再过多的说了,相信大家都熟的 ...
- 如何在github上实现预览
这个问题在网络上有很多答案,但是真正能解决的寥寥无几!接下来我就来尝试一下网络上疯传的几种方法.准备好了吗?我要开车了!!! PS:以下实验上传到github的demo采取导入本地css,js和网络上 ...
- mysql获取正在运行的sql
select id,db,host,time,info,command from information_schema.processlist where command<>'sleep' ...