Typecho plugin compilation tutorial (6): Call the interface, typecho plugin _ PHP Tutorial

Source: Internet
Author: User
Typecho plugin compilation tutorial (6): Call the interface and typecho plugin. Tutorial on compiling typecho plug-in (6): Call the interface. in this article, we start to call the interface. in the plug-in class, we define a new method named send_post, in the method, we compile the typecho plug-in tutorial (6): Call the interface, typecho plug-in

In this article, we start to call the interface. in the plug-in class, we define a new method named send_post. in the method, we get the interface call address through system configuration.

Baidu uses php CURL in the example. for more advanced usage, you can learn the PHP_cURL initialization and execution methods.

Next we will combine the code provided by Baidu webmaster.

/*** Send data ** @ param $ url the url to be sent * @ param $ options system configuration */public static function send_post ($ url, $ options) {// Obtain API $ api = $ options-> plugin ('baidusubmittest')-> api; // prepare data if (is_array ($ url )) {$ urls = $ url;} else {$ urls = array ($ url);} $ ch = curl_init (); $ options = array (CURLOPT_URL => $ api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode ("\ n", $ urls), CURLOPT_HTTPHEADER => array ('content-Type: text/plain '),); curl_setopt_array ($ ch, $ options); $ result = curl_exec ($ ch); // record the log file_put_contents ('/tmp/send_log ', date ('H: I: s '). $ result. "\ n ");}

Because we have not yet created a log system, we need to write logs to files first. let's take a look at the results first!

Return value:
The code is as follows:
{"Remain": 48, "success": 1}

Good! It seems that there is no problem! But for the sake of insurance, I still use the http class that comes with typecho to override this method.

Public static function send_post ($ url, $ options) {// Obtain API $ api = $ options-> plugin ('baidusubmittest')-> api; // prepare data if (is_array ($ url) {$ urls = $ url;} else {$ urls = array ($ url);} // to ensure successful calls, the old master first checks if (false = Typecho_Http_Client: get () {throw new Typecho_Plugin_Exception (_ t ('sorry, your host does not support php-curl extension and does not enable the allow_url_fopen function. you cannot use this function normally ');} // send the request $ http = Typecho_Http_Client: get (); $ http-> setData (implode ("\ n", $ urls); $ http-> setHeader ('content-type', 'text/plain '); $ result = $ http-> send ($ api); // record the log file_put_contents ('/tmp/send_log', date ('H: I: s '). $ result. "\ n ");}}

Now our plug-ins can basically run, but the structure can be further optimized!

Invoke (6): Call the interface, typecho plug-in this article we started to call the interface, we define a new method in the plug-in class, named send_post, in the method we...

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.