PHP发送post请求
<?php function postPHP($con){
$str=json_encode($con['data']);//如果传的不是json字符串的话需要转一下 $str=$con['data']; $context=array('http'=> array( 'method'=>'POST', 'header'=>'Content-type:application/json;charset=utf-8'."\r\n". "Authorization:Basic".base64_encode($con['userid'].":".$con['password'])."\r\n". 'Content-length'.strlen($str), 'content'=>$str ) ); $contextid=stream_context_create($context); $scok=fopen($con['url'],'r',false,$contextid); if($scok){ $result=''; while(!feof($scok)){ $result.=fgets($scok,4096); }
fclose($scok); return $result; }else{ return $str; } }
//单独写一个文件
$data=$_post['json'];/**传的json字符串格式数据,如果数据格式是json字符串时postPHP方法里就不需要json_enode了*/ $arr=array('data'=>$data,'url'=>'http://1.1.1.1'); $datas = postPHP($arr); echo $datas;