search
HomeBackend DevelopmentPHP Tutorialtypecho插件编写教程(六):调用接口_php实例

此篇我们开始调用接口,我们在插件类中新定义一个方法,起名为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");
  }

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

返回值:

复制代码 代码如下:

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

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

Statement
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
atom中 40+ 个常用插件推荐分享(附插件安装方法)atom中 40+ 个常用插件推荐分享(附插件安装方法)Dec 20, 2021 pm 04:14 PM

本篇文章给大家分享40+ 个atom常用插件,并附上在atom中安装插件的方法,希望对大家有所帮助!

vscode插件分享: 6个Vue3开发必备插件vscode插件分享: 6个Vue3开发必备插件Dec 09, 2022 pm 08:36 PM

本篇文章给大家整理分享 6 个 Vue3 开发必备的 VSCode 插件,可以直接用过 VSCode 的插件中心直接安装使用,希望对大家有所帮助!

2023年最新最全的VScode插件推荐2023年最新最全的VScode插件推荐Jan 24, 2023 am 05:30 AM

这篇文章主要介绍了这么多年来我在使用 VSCode 过程中用到的一些不错的插件。这些VSCode插件,帮你打造地表最强IDE!

【吐血总结】23个VSCode 插件,助你提高开发效率和美观性【吐血总结】23个VSCode 插件,助你提高开发效率和美观性Mar 10, 2022 pm 08:01 PM

本篇文章给大家总结了23个各种功能的VSCode 插件,可以帮助开发者提高开发效率和美观性,希望对大家有所帮助!

如何利用PHP和Typecho构建社交媒体网站如何利用PHP和Typecho构建社交媒体网站Jul 22, 2023 am 11:40 AM

如何利用PHP和Typecho构建社交媒体网站随着社交媒体的兴起,越来越多的人希望拥有一个属于自己的社交媒体网站。而搭建一个社交媒体网站的其中一种方式是使用PHP和Typecho这两个开源工具。PHP是一种被广泛应用于Web开发的脚本语言,而Typecho是一个小巧、易用、安全的博客系统,不仅功能强大,而且可以方便的进行二次开发。下面我将介绍如何使用PHP和

如何使用PHP和Typecho打造多语言支持的网站如何使用PHP和Typecho打造多语言支持的网站Jul 21, 2023 pm 11:21 PM

如何使用PHP和Typecho打造多语言支持的网站导语:随着全球化的发展,构建一个多语言支持的网站逐渐成为企业和个人所追求的目标。而PHP作为一种流行的编程语言,结合Typecho这一优秀的PHP开源博客程序,可以轻松实现多语言网站的搭建。本文将介绍如何使用PHP和Typecho来打造一个多语言支持的网站,并提供相关的代码示例。一、安装和配置Typecho首

浏览器增强版ChatGPT无敌了?超强插件Monica,能聊能写效率Max浏览器增强版ChatGPT无敌了?超强插件Monica,能聊能写效率MaxMay 03, 2023 pm 06:58 PM

提起Monica,你会想到什么?是老友记里的主角之一Monica·Geller,一个热心肠的女主人形象;还是心跳文学部里的疯疯癫癫的Monika?或者,最近爆火的Chrome插件——Monica。它的功能实在是太强大了,用完一次保你爱不释手。毕竟,搭载了ChatGPT的网页助手,能是俗物吗?Monica功能大赏首先明确一点,Monica是ChatGPT在网页上的应用,换句话说,Monica就是靠着ChatGPTAPI的强大功能才厉害。而仔细看看Chrome商店中的介绍,我们就会发现Monica真

30个实用VSCode 插件,让你的开发效率倍增!30个实用VSCode 插件,让你的开发效率倍增!Apr 24, 2022 pm 09:12 PM

本篇文章给大家总结分享30个实用VSCode 插件,让你的日常开发工作效率倍增,希望对大家有所帮助!

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version