This article mainly introduces the typecho plug-in compiling tutorial (6): Call the interface. this is the last article in the series. if you need it, refer to the next article to start calling the interface, in the plug-in class, we define a new method named send_post. in the method, we obtain 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!