用 php 编写简单的 api 接口

编写 api 接口可以开放给别人方便调用数据,与前端交互等, “php 是世界上最好的语言” ,哈哈。。。下面记录一下 php 写接口用到的一些东西,便于以后直接 copy,嗯...程序员的工作就是 Ctrl+C and Ctrl+V 。

<?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('呜啦啦啦啦啦啦。。。');
}
转载请注明出处。
何先生 » 用 php 编写简单的 api 接口
Loading...

发表评论

表情
图片 链接 代码

提供最优质的资源集合

立即查看 了解详情