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;

 

posted @ 2020-12-04 21:29  不吃肉的羊  阅读(2529)  评论(0编辑  收藏  举报