赞
踩
前排说重点:关键方法在context.php文件里,有关于第三方接口调用的方法 function api_execute。 主要是在传递参数时,如果选择json格式传递,需要做相关处理; 然后在api_execute方法里,也需要做参数的相关格式设置。
首先是本地跑起一个java项目,模拟第三方接口的提供:
- @PostMapping("/testLogin")
- public Map test(@RequestBody Map mapData) {
-
- Map<String, String> map = new HashMap<>();
- System.out.println("获取的值:" + mapData.toString());
- if (mapData.get("username").equals("admin") && mapData.get("pwd").equals("123456")) {
- map.put("code", "200");
- map.put("data", "login success");
- }else {
- map.put("code", "-1");
- map.put("data", "login fail");
- }
- return map;
- }
OK,接下来是PHP相关代码:
显示目录结构:
场景流程:
buttonTest.php 模拟用户输入账户名和密码,点击按钮进行登录
buttonTest.php 带着参数触发context.php,将参数传递过去
context.php带着参数,调用方法去访问第三方接口API
第三方接口API收到参数后,做登录校验处理,返回参数
context.php 接收第三方接口API的参数,做逻辑处理(数据库写入等),带着结果参数跳转main.php
最后main.php对结果参数做处理
buttonTest.php:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- </head>
- <body>
- <form method="post" action="context.php">
- username: <input type="text" name="username">
- pwd: <input type="text" name="pwd">
-
- <input type="submit" value="login">
- </form>
- </bosy>
- </html>
context.php:
- <?php
- $username=$_POST["username"]; //传递过来的username
- $pwd=$_POST["pwd"]; //传递过来的pwd
-
- $data_json = array("username" => $username, "pwd" => $pwd); //将参数拼接成json
- $data = json_encode($data_json); //模拟调用接登录接口参数
-
- $url="http://localhost:8088/testLogin";//模拟登录接口
- $method="POST";
- $result= api_execute($method,$url,$data);//获取接口返回值
- $result_json= json_decode($result, true);
- $code=$result_json['code'];
- $data=$result_json['data'];
- $mysql_result=user_insert();//调用user插入方法,保存用户信息
- echo "CODE: $code <br>
- DATA: $data <br>";
- echo "$mysql_result<br>AAA";
- if ($mysql_result="success") {
- $a1="login success,welcome to the mainHtml !";
- // require ("../studyDay/main.php");
- header('Location:http://localhost/studyDay/main.php?n='.$a1);
-
- }
-
-
- //用户数据插入
- function user_insert(){
-
- $mysql_host = "localhost";
- $mysql_username = "root";
- $mysql_password = "root";
- $mysql_dbname = "discuztest";
-
- // 创建连接
- $conn = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_dbname);
- // 检测连接
- if ($conn->connect_error) {
- die("连接失败: " . $conn->connect_error);
- }
-
- $uid=10;
- $username="JCccc";
- $password="1223344";
- // $password="123456";
- $email="861122334@qq.com";
- $regip="127.0.0.1";
- $regdate="1567748889";
- $salt="9a170e";
- $sql = "INSERT INTO pre_ucenter_members (uid,username, password,email,regip,regdate,salt)
- VALUES ('$uid','$username', '$password','$email','$regip','$regdate','$salt')";
-
- if ($conn->query($sql) === TRUE) {
- return "success";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- return "fail";
- }
-
- $conn->close();
- }
-
- //api接口调用
- function api_execute($method, $url, $data = false) {
- $curl = curl_init();//初始化curl
- switch ($method)
- {
- case "POST":
- curl_setopt($curl, CURLOPT_POST, 1);
- if ($data)
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- break;
- case "PUT":
- curl_setopt($curl, CURLOPT_PUT, 1);
- break;
- default:
- if ($data)
- $url = sprintf("%s?%s", $url, http_build_query($data));
- }
- // Optional Authentication:
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array( /*设置请求头*/
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data))
- );
- $result = curl_exec($curl);
- curl_close($curl); //关闭 curl
- return $result;
- }
-
- ?>
-
-
-
main.php:
- <?php
- $a=$_REQUEST['n'];
- echo $a;
- ?>
测试结果:
数据库:
java第三方接口:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。