It's our wits that make us men.

php接口开发

Posted on By lk

php接口开发

http://localhost/openUser.php?act=get_user_list&type=json

在这里openUser.php相当于一个接口,其中get_user_list是一个API(获取用户列表),

讲求返回的数据类型为JSON格式。

你只需要在你PHP代码中执行这条链接他就会返回。

GET方式的直接使用

$file_contents = file_get_content(‘http://localhost/openUser.php?act=get_user_list&type=json’)

POST方式得用下面的(需要开启PHP curl支持)。

$url = ‘http://localhost/openUser.php?act=get_user_list&type=json’;

$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 );