初步学习使用PHP写API接口的知识

25December

10:16 AM

PHP

API

259 次浏览

0 条评论

之前忙于工作,最近闲下来后准备继续搞我的API接口。处于编程语言的限制,我还是喜欢用PHP来写API的对接,买了一本书,看了以后一头雾水。

PHP写API接口依旧是遵循http的GET和POST协议方式,比较常用的一种方法就是开启一个curl。目前,libcurl目前支持http、https、ftp、gopher、telnet、dict、file和ldap协议。libcurl同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。

注意是的这个对于PHP的版本有一定的要求,所以使用前请充分了解相关信息:https://www.runoob.com/php/php-ref-curl.html

查阅了一些相关资料,也找了一两个可以参考的案例放在下面,以便后期参考使用:

<?php
header('Content-type:text/json');
header('Access-Control-Allow-Origin:*');//指定允许其他域名访问
//header('Access-Control-Allow-Methods:GET');//响应类型
//header('Access-Control-Allow-Headers:x-requested-with,content-type');//响应头设置
 
$output = array();
$a = @$_GET['a'] ? $_GET['a'] : '';
//$a = isset($_GET['a']) ? addslashes($_GET['a']) : '';
 
if (empty($a)) {
    $output = array("code"=>444,"msg"=>"no","data"=>NULL);
    exit(json_encode($output));
}
 
if ($a == 'test') {
    //api.php?a=test&name=jianggle&phone=66669999&content=我是内容
    //响应 _GET
    $name = isset($_GET['name']) ? addslashes(trim($_GET['name'])) : '';
    $phone = isset($_GET['phone']) ? addslashes(trim($_GET['phone'])) : '';
    $content = isset($_GET['content']) ? addslashes(trim($_GET['content'])) : '';
    //响应 _POST
    //$name = isset($_POST['name']) ? addslashes(trim($_POST['name'])) : '';
    //$phone = isset($_POST['phone']) ? addslashes(trim($_POST['phone'])) : '';
    //$content = isset($_POST['content']) ? addslashes(trim($_POST['content'])) : '';
    if(!empty($name)&&!empty($phone)&&!empty($content)){
            $arr1 = array("name"=>$name,"phone"=>$phone,"content"=>$content);
            $output = array("code"=>666,"msg"=>"yes","data"=>$arr1);
            exit(json_encode($output));
    }else{
            $output = array("code"=>666,"msg"=>"no","data"=>NULL);
            exit(json_encode($output));
    }
}else if($a == 'transfer'){
    //此法可临时解决跨域问题,仅适用于 get 方式
    //api.php?a=transfer&params=user/profile&token=tokenkenkenken
    $abc = '';
    while (list($key, $val) = each($_GET)){
        $abc .= '&'.$key.'='.$val;
    }
    $last_sth = str_replace("&a=transfer&params=","",$abc);
    $last_url = str_replace($_GET['params']."&",$_GET['params']."?",$last_sth);
    $content = file_get_contents('http://127.0.0.1/testSth/'.$last_url);
    exit($content);
}else if($a == 'transfer_post'){
    //仅做保存,暂未使用过
    //post 方式(需要开启 PHP curl 支持)。
    $url = 'http://127.0.0.1/testSth/';
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
    curl_setopt ( $ch, CURLOPT_POST, 1 ); //启用 POST 提交
    $file_contents = curl_exec ( $ch );
    curl_close ( $ch );
}else if($a == 'getSlider'){
    //api.php?a=getSlider&num=2
    //没事写着测点东西的。。。
    $num = isset($_GET['num']) ? addslashes(trim($_GET['num'])) : '';
    $arr = array(
        1=>array("id"=>1,"url"=>"#/11","img"=>"statics/images/1.jpg"),
        2=>array("id"=>2,"url"=>"#/22","img"=>"statics/images/2.jpg"),
        3=>array("id"=>3,"url"=>"#/33","img"=>"statics/images/3.jpg"),
        4=>array("id"=>4,"url"=>"#/44","img"=>"statics/images/4.jpg")
    );
    if($num==1){
        $output = array("code"=>666,"data"=>array($arr[1]));
    }else if($num==2){
        $output = array("code"=>666,"data"=>array($arr[1],$arr[2]));
    }else if($num==3){
        $output = array("code"=>666,"data"=>array($arr[1],$arr[2],$arr[3]));
    }else if($num==4){
        $output = array("code"=>666,"data"=>array($arr[1],$arr[2],$arr[3],$arr[4]));
    }else{
        $output = array("code"=>666,"data"=>NULL);
    }
    exit(json_encode($output));
}else{
    die('呜啦啦啦啦啦啦。。。');
}

还有一个参考案例,他是自己做了一个数据接口,比较方便:http://www.naipan.com/api_http.html

留言评论
称呼
邮箱
网址
  • 招投标  : 博客很棒 欢迎回访我哦
展开