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. bzoj2000 [Hnoi2010]stone 取石头游戏

    Description A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参加比赛. 与经典的取石子游戏相 ...

  2. mysql.sock问题

    Can't connect to local MySQL server through socket '/tmp/mysql.sock' 上述提示可能在启动mysql时遇到,即在/tmp/mysql. ...

  3. 创建blob地址

    aa="121" "121" b=new Blob([aa]) Blob(3) {size: 3, type: ""} window.URL ...

  4. idea debug操作

    3. 条件断点 说明: 调试的时候,在循环里增加条件判断,可以极大的提高效率,心情也能愉悦.具体操作: 在断点处右击调出条件断点.可以在满足某个条件下,实施断点. 查看表达式的值(Ctrl + u): ...

  5. 时钟晶振32.768KHz为什么是15分频?

    实时时钟晶振为什么选择是32768Hz的晶振,在百度上搜索的话大部分的答案都是说2的15次方是32768,使用这个频率的晶振,人们可以很容易的通过分频电路得到1Hz的计时脉冲.但是话有说回来了,2的整 ...

  6. Winodws SNMP服务安装和配置(Windows 2003 & 2008 R2)

    简单网络管理协议SNMP服务起着代理的作用,它会收集可以向SNMP管理站或控制台报告的信息.您可以使用SNMP服务来收集数据,并且在整个公司网络范围内管理基于Windows Server 2003.M ...

  7. one or more listeners failed to start问题解决思路

    今日搭建一个web应用的时候总是遇到tomcat报错:one or more listeners failed to start. Full detail balabale....而且还没有其他提示, ...

  8. webpack之理解loader

    我们在写webpack配置文件的时候,应该有注意到经常用到loader这个配置项,那么loader是用来做什么的呢? loader其实是用来将源文件经过转化处理之后再输出新文件. 如果是数组形式的话, ...

  9. Mysql基于Linux上的安装

    MySQL 在Linux/Unix安装 所有平台的 MySQL 下载地址为: MySQL 下载 . 挑选需要的 MySQL Community Server 版本及对应的平台. 注意:安装过程需要通过 ...

  10. 常用关于时间的一些设置。获取当前时间后30天;判断时间段一年内;Date转String,String转Date

        //获取当前时间后30天(之前也可),天数不限可修改1 var data = new Date(); var date1 = newe Date(date); date2 = date1.se ...