Home Backend Development PHP Tutorial typecho插件编写教程(六):调用接口_PHP

typecho插件编写教程(六):调用接口_PHP

May 30, 2016 am 08:47 AM
typecho plug-in

此篇我们开始调用接口,我们在插件类中新定义一个方法,起名为send_post,在方法中我们通过系统配置获取接口调用地址。

百度给的例子中使用了php的CURL,更高级的使用方法可以学习PHP_cURL初始化和执行方法

下面我们结合一下百度站长提供的代码。

/**
   * 发送数据
   * @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");
  }

Copy after login

由于我们还没有建立日志系统,所以我们将日志先写入文件,先看效果吧!

返回值:

代码如下:


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


Good!看来没有什么问题!不过为了保险起见,我还是用typecho自带的http类重写了此方法。

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

Copy after login

现在我们的插件基本能够运行了,但是在结构上还可以进一步优化!

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 Article Tags

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)

PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! Feb 25, 2024 pm 11:57 PM

PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation!

Error loading plugin in Illustrator [Fixed] Error loading plugin in Illustrator [Fixed] Feb 19, 2024 pm 12:00 PM

Error loading plugin in Illustrator [Fixed]

What is the Chrome plug-in extension installation directory? What is the Chrome plug-in extension installation directory? Mar 08, 2024 am 08:55 AM

What is the Chrome plug-in extension installation directory?

Share three solutions to why Edge browser does not support this plug-in Share three solutions to why Edge browser does not support this plug-in Mar 13, 2024 pm 04:34 PM

Share three solutions to why Edge browser does not support this plug-in

Does PyCharm Community Edition support enough plugins? Does PyCharm Community Edition support enough plugins? Feb 20, 2024 pm 04:42 PM

Does PyCharm Community Edition support enough plugins?

How to use WordPress plug-in to implement video playback function How to use WordPress plug-in to implement video playback function Sep 05, 2023 pm 12:55 PM

How to use WordPress plug-in to implement video playback function

How to add online ordering functionality to WordPress plugin How to add online ordering functionality to WordPress plugin Sep 05, 2023 pm 03:42 PM

How to add online ordering functionality to WordPress plugin

How to add WeChat mini program functionality to WordPress plug-in How to add WeChat mini program functionality to WordPress plug-in Sep 06, 2023 am 09:03 AM

How to add WeChat mini program functionality to WordPress plug-in

See all articles