Typecho Plugin Authoring Tutorial (vi): Calling interface, Typecho plugin _php tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.