Typecho Plugin Authoring Tutorial (vi): Calling interface, Typecho plugin
In this article we start to call the interface, we define a new method in the plug-in class, named Send_post, in the method we get the interface call address through the system configuration.
Baidu gave the example of using PHP curl, more advanced use of the method can learn Php_curl initialization and execution methods
Below we combine the Baidu Webmaster to provide code.
/** * Send data * @param $url ready to send URL * @param $options System Configuration * /public static function Send_post ($url, $o ptions) { //Get 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,< C18/>curlopt_postfields = implode ("\ n", $urls), Curlopt_httpheader = = Array (' Content-type:text/plain '), ); Curl_setopt_array ($ch, $options); $result = curl_exec ($ch); Log file_put_contents ('/tmp/send_log ', date (' H:i:s '). $result. "\ n"); }
Since we have not established the log system, so we will write the log file first, look at the effect!
return value:
Copy the Code code as follows:
{"Remain": $, "Success": 1}
Good! looks like there's no problem! But for the sake of insurance, I have rewritten this method with the HTTP class that Typecho comes with.
public static function Send_post ($url, $options) { //Get API $api = $options->plugin (' baidusubmittest ') API; Prepare data if (Is_array ($url)) { $urls = $url; } else{ $urls = Array ($url); } In order to ensure a successful invocation, the old Gao first made a judgment if (false = = Typecho_http_client::get ()) { throw new Typecho_plugin_exception (' _t (' Sorry, Your host does not support the Php-curl extension and does not have the Allow_url_fopen feature open, this feature cannot be used properly ') } Send request $http = Typecho_http_client::get (); $http->setdata (Implode ("\ n", $urls)); $http->setheader (' Content-type ', ' text/plain '); $result = $http->send ($API); Log file_put_contents ('/tmp/send_log ', date (' H:i:s '). $result. "\ n");} }
Now our plug-in basically can run, but the structure can be further optimized!
http://www.bkjia.com/PHPjc/1008001.html www.bkjia.com true http://www.bkjia.com/PHPjc/1008001.html techarticle Typecho Plugin Authoring Tutorial (vi): Call interface, Typecho plug-in this chapter we start to call the interface, we in the plug-in class new definition of a method, named Send_post, in the method we ...