php开发接口

发布时间:2019-08-29作者:小灵龙点击:129

问题描述:

  我公司的网站需要对接一家公司的接口,把人家的数据调用过来。对方技术给了一份接口文档。以前没接触过接口,反正搞明白了一点,写下流程。

解决办法:


第一步:请求地址的操作。

       在网站根目录下建立loginCallBack目录,目录里面创建index.php文件。这样打开http://(自己网站域名)/loginCallBack就可以打开这个index.php文件了。当然在目录后面输入index.php也能打开。那么这个请求地址就做好。

第二步:请求参数及请求HTTP头信息的操作。

这个user_token是人家发送过来的,我们php文件里面要接收这个参数,通过我们验证后,就可以调用我们的数据了。所以怎么在php文件中接收这个post参数呢?
1,如果请求HTTP头信息是json传送过来的。
使用
$json_string=file_get_contents("php://input");//获取postman发送过来的数据流,再通过:

$data = json_decode($json_string) ;//将json转为对象Object,后使用类似$data->account方式访问元素数据

$data = json_decode($json_string, true); //将json转为数组Array,后使用$data["account"]方式访问元素数据

如果请求不到。
查看php.ini中allow_url_fopen是否开启?

2,如果请求HTTP头信息是form-data方式传送过来的。
使用 $_POST接收post参数user_token就可以了。

第三步:在php文件中发送数据给别人。

接收user_token后,并且验证通过后就需要使用json方式发送数据了。


$sql="SELECT mid,userid,rank,email,company_name,sou_expire FROM `boke_member` WHERE mid= ".$userid;

   

    $row=$dsql->GetOne($sql);   

    $meals[]=array(

        'type' => 'A',

    );

    $arr []= array(



        'uid'          => $row['mid'],

        'username'     => $row['userid'],

        "role"         => "mainaccount",

        "email"        => $row['email'],

        'company_name' => iconv('gb2312','utf-8',$row['company_name']),(如果输出的中文是null,则需要使用函数iconv转码)

        "expiry_time"  => $row['sou_expire'],

        'meals'        => $meals,

     

       

    );
 function json($code,$suc=true,$message='',$data=array()){

        if(!is_numeric($code)){

            return '';

        }

        $result = array(

			'error_code'=>$code,
			'success'=>$suc,

			'message'=>$message,

            'data'=>$data

        );
		
        echo json_encode($result);

        exit;

    }
json(0,true,"数据返回成功",$arr);

第四步:测试,发送token并接收数据。

在postman上或者其他自家网站上建立test.php文件。

function request_post($url = '', $post_data = array()) {

        if (empty($url) || empty($post_data)) {

            return false;

        }

      



        $postUrl = $url;

        $curlPost = $post_data;

        $ch = curl_init();//初始化curl

        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页

        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上

        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式

        curl_setopt($ch, CURLOPT_ENCODING, "");//解压

        //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate,flate'));

        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书下同

        $data = curl_exec($ch);//运行curl

        curl_close($ch);

         //$res=json_decode($data,true);

        return $data;

    }

   

   

    function testAction(){

        $url = 'http://www.xxxx.com/loginCallBack/index.php';

        $post_data['user_token']       = '92c65c183ef58f2ef45a3a1af5080a';

      

           $post_data=json_encode($post_data);

        $res = request_post($url, $post_data);      

       echo $res;

    }

testAction();


标签:php接口