当前位置:   article > 正文

PHP调用第三方API_php调用第三方api接口

php调用第三方api接口

1、实例

<?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;
}   
 
?>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/270112
推荐阅读
相关标签
  

闽ICP备14008679号