tp6远程访问另一个服务器数据

标题:服务器1访问服务器2的数据

服务器1

  //测试获取远程服务器信息
    public function getinfo()
    {
        $unionid='';
        $res=$this->getserverinfo($unionid);
        echo '<pre>';
        print_r($res);
    }
    //远程获取服务器上的信息
    public  function getserverinfo($unionid='')
    {
        $ser=Config('app.serverdomain');
        $url=$ser.'/api/index/getinfo/';
        //echo '<pre>';
        //echo $url;
        $data=[];
        $data['unionid']=$unionid;
        $result=curl_call($url,'post',$data);
        $result=json_decode($result,true);
        if($result['No']=='0')
        {
            $this->BackResult('6', '您必须先在日拱一卒小程序成为会员');
        }
        return $result;
    }
//CUrl请求调用接口
function curl_call($url,$method='get',$data='')
{
    $ch = curl_init();
    $header = array('Accept-Charset: utf-8');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if($error=curl_error($ch)){
        return $error;
    }
    curl_close($ch);
    return $result;
}

服务器2

  //获取用户信息
    public function  getinfo()
    {
        // echo '<pre>';
        // print_r($_POST);
        // exit;
        $unionid=$_POST['unionid']?$_POST['unionid']:'';
        $back=[];
        if($unionid)
        {
            $info=DB::name('member')->field('member_id,openid,member_true_name')->where(['unionid'=>$unionid])->find();
            if(empty($info['member_id'])){
                $back['No']=0;
            }else{
                $totaljuanintegral=DB::name('juanintegral')->where(['openid'=>$info['openid']])->sum('giveintegarl');
                $back['No']=1;
                $back['membername']=$info['member_true_name'];
                $back['totaljuanintegral']=$totaljuanintegral;
            }
        }
         $res=json_encode($back,JSON_FORCE_OBJECT);
         //file_put_contents('./getinfo.txt',$res);
         return  $res;
    }

问题:curl获取到的结果有tp6返回的debug调试错误信息,不能json_decode,
解决办法:
在服务器2的环境配置文件中(.env)将APP_DEBUG设置为false,但是还想看到错误信息,需要在config/app.php中将show_error_msg设置为true,这样就hi则返回json数据了。

上一篇:LotusAdmin帮开发者节省两天开发时间_thinkphp6 admin (博主原创)


下一篇:iOS开发数据库篇—SQLite的应用