php简单编写api接口
[ 2018/02/02, 后端技术 , 6357阅, 0评 ]

前端开发中经常需要进行模拟数据交互,所以简单学了些php知识,因为“php是世界上最好的语言”嘛,哈哈。。。下面记录一下php写接口用到的一些东西,便于以后直接copy,hiahiahia...

<?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('呜啦啦啦啦啦啦。。。');
}

有朋自远方来...评论一下呗O(∩_∩)O