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;
}

随机推荐

  1. bzoj2336 [HNOI2011]任务调度

    Description 正解:搜索+随机化. 先写个搜索,枚举所有没有要求的任务属于哪一种任务,然后再用爬山来更新最优解. 具体来说就是先把所有先做任务$A$的按照$a$时间从大到小排序,先做任务$B ...

  2. 【转】Android之drawable state各个属性详解

    我们在定义一个drawable的时候可以通过xml定义的drawable对象.它使得一个图片能在不同的状态下显示不同的图案,比如一个Button,它有pressed,focused,或者其它状态,通过 ...

  3. Google Map中的瓦片

    一.墨卡托投影google map使用的是EPSG:900913标准墨卡托投影(等角圆术地图投影)y = R*ln(tan(pi/4 + a/2))x = R*b当y等于piR时,投影图正好为一个正方 ...

  4. 由.def文件生成lib文件[转]

    最近在学习curl库时,碰到一个问题,从官网上下载了一个lib版的,却发现只有.dll,没有lib文件,感觉很奇怪,google了之后才知道,原来库作者的用意是让用户自己生成lib文件,下载到的lib ...

  5. mongo数据库基础语法

    http://www.runoob.com/mongodb/mongodb-create-collection.html 很详细  

  6. 【题解】洛谷P2023 [AHOI2009] 维护序列(线段树)

    洛谷P2023:https://www.luogu.org/problemnew/show/P2023 思路 需要2个Lazy-Tag 一个表示加的 一个表示乘的 需要先计算乘法 再计算加法 来自你谷 ...

  7. .net 网站中如何动态播放音乐,页面如何播放音乐

    向别人请教有好处也有坏处,好处是你可以相对比较快的知道要点,坏处就是你TM的发现你弄了那么久都是白弄. 昨天今天一直在找一个问题的解决方案,我的问题描述大概是这样子的:我用vs2012开发的.net网 ...

  8. mysql错误errno:121

    121错误是因为外键名重复.在同一个库中外键是不允许与其他外键重名的. 遇到这个错误请给你定义的外键换唯一无重复的名字. 同时查阅到外键也有可能导致150错误. Can't create table ...

  9. Python 学习笔记(十)Python集合(三)

    集合运算 元素与集合的关系 元素与集合的关系 ,就是判断某个元素是否是集合的一员."a" in aset >>> s =set([1,2,3,4]) >&g ...

  10. mysql获取正在运行的sql

    select id,db,host,time,info,command from information_schema.processlist where command<>'sleep' ...