Tutorial on compiling typecho plug-in (6): Call the interface. Typecho plugin compilation tutorial (6): calling interfaces this article mainly introduces the typecho plugin compilation tutorial (6): calling interfaces, this is the last article in the series, if you need it, refer to the typecho plug-in compiling tutorial (6): Call the interface
This article mainly introduces the typecho plugin compiling tutorial (6): Call the interface. this is the last article in the series. For more information, see
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.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
/**
* Send data
* @ Param $ url the url to be sent
* @ Param $ options system configuration
*/
Public static function send_post ($ url, $ options ){
// Obtain the 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 logs
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.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
Public static function send_post ($ url, $ options ){
// Obtain the 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 makes a judgment
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, so this function cannot be used properly '));
}
// Send the request
$ Http = Typecho_Http_Client: get ();
$ Http-> setData (implode ("\ n", $ urls ));
$ Http-> setHeader ('content-type', 'text/plain ');
$ Result = $ http-> send ($ api );
// Record logs
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!
Callback (6): Call the interface 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...