Php uses curl to call interface methods, get and post, and curl.
The first is the client execution method ApiModel. php:
<? Php/*** simulate post url request * @ param string $ url * @ param array $ post_data */function request_post ($ url = '', $ ispost = true, $ post_data = array () {if (empty ($ url) | empty ($ post_data) {return false;} $ o = ""; foreach ($ post_data as $ k => $ v) {$ o. = "$ k = ". urlencode ($ v ). "&" ;}$ post_data = substr ($ o, 0,-1); $ key = md5 (base64_encode ($ post_data); if ($ ispost) {$ url = $ url;} else {$ url = $ url. '? '. $ Post_data;} $ curlPost = 'key = '. $ key; header ("Content-type: text/html; charset = UTF-8"); $ ch = curl_init (); // initialize curl curl_setopt ($ ch, CURLOPT_URL, $ url); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HEADER, 0); // set header curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // The result must be a string and be output to the screen. if ($ ispost) {curl_setopt ($ ch, CURLOPT_POST, 1); // The post submission method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost);} $ data = curl_exec ($ c H); // run curl curl_close ($ ch); return $ data ;}?>
The call method of the client. You can configure the basic information api. php here:
<? Php require 'apimodel. php '; function testAction () {$ url = 'interface address'; $ post_data ['appid'] = '10'; $ post_data ['appkey'] = 'cmbohpffxvr03nipkqxaaa1vf5no4nq '; $ post_data ['Member _ name'] = 'zsjs124'; $ post_data ['Password'] = '000000'; $ post_data ['email '] = 'zsjs124 @ 126.com '; // $ post_data = array (); $ res = request_post ($ url, $ ispost = true, $ post_data); print_r ($ res);} testAction ();?>
Server interface function test. php:
<? Php function serverapi () {$ key = '57173d6ad842d807443ee0db91323 323 '; if ($ _ GET & $ _ GET ['appkey'] = 'cmbohpffxvr03nipkqxaaa1vf5no4nq '| $ _ POST & $ _ POST ['key'] = $ key) {$ arr = array ('name' => 'hangzhou', 'Password' => '000000'); echo json_encode ($ arr );} else {exit ('illegal access! ') ;}} Serverapi () ;?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.