Home > Backend Development > PHP Tutorial > How to write an interface in php

How to write an interface in php

WBOY
Release: 2016-07-29 09:12:30
Original
1896 people have browsed it

I have been writing websites for a year, mostly PC-side websites. Recently, I changed my company and have an APP business, which involves interface issues, so I found some information and wrote some project interfaces. Remember it, write it down, and read it later.

The APP interface itself is not difficult. It is just like writing an ordinary website. However, the value assigned to the HTML page in the controller becomes an interface that outputs json data. The following is the code:

public function jiekou(){//查询
	$User = M('User');
	$list = $User->select();//查询数据
	$return['list'] = $list;
	echojson($return);//接口返回的json数据了
}
Copy after login
public function delall()//删除
{
	$User = M('User');
	$map['id']=1;
	if ($User->where($map)->delete())
	{
		$return['code'] = 200;
		$return['msg'] = '删除成功';
		echojson($return);
	}
	else
	{
		$return['code'] = 400;
		$return['msg'] = '删除失败';
		echojson($return);
	}
}
Copy after login

Of course, I have seen many The output format, for example: echo json_encode($return); There is no problem. The test tool I use is the Google plug-in Postman, which can be searched in the Google App Store. If you cannot open the App Store, you can first download a Google hosts file online and update it on your local computer. The above introduces how to write interfaces in PHP, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template