Php uses curl to call interface methods, get and post, and curl.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.