Table of Contents
Typecho plug-in writing tutorial (6): Calling the interface
Home Backend Development PHP Tutorial Typecho plug-in writing tutorial (6): calling interface_PHP tutorial

Typecho plug-in writing tutorial (6): calling interface_PHP tutorial

Jul 13, 2016 am 09:52 AM
typecho main interface plug-in Tutorial article write transfer

Typecho plug-in writing tutorial (6): Calling the interface

This article mainly introduces the typecho plug-in writing tutorial (6): Calling the interface. This is the last article in the series. , friends in need can refer to it

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 obtain the interface calling address through the system configuration.

The example given by Baidu uses php's CURL. For more advanced usage, you can learn the PHP_cURL initialization and execution method

Now let’s 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

/**

* 发送数据

* @param $url 准备发送的url

* @param $options 系统配置

*/

public static function send_post($url, $options){

//获取API

$api = $options->plugin('BaiduSubmitTest')->api;

 

//准备数据

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);

 

//记录日志

file_put_contents('/tmp/send_log', date('H:i:s') . $result . "n");

}

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){ //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, CURLOPT_POSTFIELDS => implode("n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); //Record log file_put_contents('/tmp/send_log', date('H:i:s') . $result . "n"); }

Since we haven’t established a logging system yet, we will write the log to a file first and see the effect first!

Return value:

Copy the code. The code is as follows:

 {"remain":48,"success":1}

Good! It seems there is no problem! But just to be on the safe side, I overridden this method using the http class that comes with typecho.

 ?

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){

//获取API

$api = $options->plugin('BaiduSubmitTest')->api;

 

//准备数据

if( is_array($url) ){

$urls = $url;

}else{

$urls = array($url);

}

 

//为了保证成功调用,老高先做了判断

if (false == Typecho_Http_Client::get()) {

throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'));

}

 

//发送请求

$http = Typecho_Http_Client::get();

$http->setData(implode("n", $urls));

$http->setHeader('Content-Type','text/plain');

$result = $http->send($api);

 

//记录日志

file_put_contents('/tmp/send_log', date('H:i:s') . $result . "n");

}

}

1

2 3

45 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){ //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 call, Lao Gao made a judgment first if (false == Typecho_Http_Client::get()) { throw new Typecho_Plugin_Exception(_t('Sorry, your host does not support the php-curl extension and the allow_url_fopen function is not turned on, so this function cannot be used normally')); } //Send request $http = Typecho_Http_Client::get(); $http->setData(implode("n", $urls)); $http->setHeader('Content-Type','text/plain'); $result = $http->send($api); //Record log file_put_contents('/tmp/send_log', date('H:i:s') . $result . "n"); } }
Now our plug-in can basically run, but the structure can be further optimized! http://www.bkjia.com/PHPjc/1007648.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1007648.htmlTechArticleTypecho plug-in writing tutorial (6): Calling interface This article mainly introduces the typecho plug-in writing tutorial (6): Calling the interface, this is the last article in the series of articles, friends in need can refer to it...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

A simple tutorial on converting full-width English letters to half-width letters A simple tutorial on converting full-width English letters to half-width letters Mar 25, 2024 pm 09:21 PM

When using a computer to input English, sometimes we encounter the difference between full-width English letters and half-width English letters. Full-width English letters refer to the characters input by pressing the Shift key and the English letter key combination when the input method is Chinese mode. They occupy a full-width character width. Half-width English letters refer to characters input directly when the input method is English mode, and they occupy half a character width. In some cases, we may need to convert full-width English letters to half-width letters. Here is a simple tutorial: First, open a text editor or any

Introduction to PHP interfaces and how to define them Introduction to PHP interfaces and how to define them Mar 23, 2024 am 09:00 AM

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

Application of interfaces and abstract classes in design patterns in Java Application of interfaces and abstract classes in design patterns in Java May 01, 2024 pm 06:33 PM

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

How Google Chrome allows animation plugins to run How Google Chrome allows animation plugins to run Mar 28, 2024 am 08:01 AM

How does Google Chrome allow animation plugins to run? Google Chrome is very powerful. Many friends like to use this browser to watch video animations. However, if you want to watch various animated videos, you need to install animation plug-ins in the browser. Many friends use Google Chrome. After installing the animation plug-in, I still cannot care about the video. How should I deal with this problem? Next, let the editor show you the specific steps to allow the animation plug-in to run in Google Chrome. Friends who are interested can come and take a look. Specific steps for Google Chrome to allow animation plug-ins to run: 1. First run Google Chrome on your computer, and click the main menu button in the upper right corner of the homepage (as shown in the picture). 2. After opening the main menu, select the "Settings" option below (as shown in the picture). 3. In settings

How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer Jul 16, 2024 pm 09:02 PM

The expansion of the virtual market is inseparable from the circulation of virtual currency, and naturally it is also inseparable from the issue of virtual currency transfers. A common transfer error is the address copy error, and another error is the chain selection error. The transfer of virtual currency to the wrong chain is still a thorny problem, but due to the inexperience of transfer operations, novices often transfer the wrong chain. So how to recover the wrong chain of virtual currency? The wrong link can be retrieved through a third-party platform, but it may not be successful. Next, the editor will tell you in detail to help you better take care of your virtual assets. How to retrieve the wrong chain of virtual currency? The process of retrieving virtual currency transferred to the wrong chain may be complicated and challenging, but by confirming the transfer details, contacting the exchange or wallet provider, importing the private key to a compatible wallet, and using the cross-chain bridge tool

See all articles