Home  >  Article  >  Backend Development  >  How to write api interface in php?

How to write api interface in php?

藏色散人
藏色散人Original
2019-05-08 15:25:0217233browse

How to write api interface in php?

For PHP students, they rarely come into contact with APIs, so they are at a loss as to how to write. In fact, developing APIs is simpler than developing WEB, but the logic may be more complicated because APIs In fact, it is data output without rendering the page, so there is no MVC (the API only has M and C). Just like WEB development, you first need some relevant parameters. These parameters will be passed by the client, maybe GET or maybe POST, this requires the development team to agree with each other, or to formulate unified specifications.

How to write api interface in php?

With parameters, data processing can be completed according to application requirements, such as: task progress updates, in-APP purchases, game end data submission, etc. After the data logic is processed, the relevant data needed by the client is returned, such as: task status, in-app purchase results, player information, etc. How to return data to the client? Direct output form, such as: JSON, XML, TEXT, etc.

After the client obtains the data returned by you, it will temporarily write a simple API to interact with the user locally on the client. I hope it can help you!

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

The above is the detailed content of How to write api interface in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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