wordpress本地开发_适用于WordPress开发人员的XML-RPC

wordpress本地开发

XML-RPC is a remote procedure call (one process calling the function of another process via a remote connection) protocol which uses XML to represent data and HTTP to make the calls. Although applications can explicitly provide their own REST APIs for RPC, a standard protocol can help security and provide many other benefits. For example, developers don’t have to design a REST API architecture from scratch and also a single client can be used to make remote procedure calls to various server applications which support the standard protocol. Therefore, XML-RPC was introduced as a standard protocol for RPC.

XML-RPC是一种远程过程调用(一个过程通过远程连接调用另一个过程的功能)协议,该协议使用XML表示数据,并使用HTTP进行调用。 尽管应用程序可以为RPC显式提供自己的REST API,但是标准协议可以帮助提高安全性并提供许多其他好处。 例如,开发人员不必从头开始设计REST API架构,也可以使用单个客户端对支持标准协议的各种服务器应用程序进行远程过程调用。 因此,XML-RPC被引入作为RPC的标准协议。

XML-RPC for WordPress

In this tutorial we’ll look at the different core WordPress functions which can be executed remotely using XML-RPC. This can help us to build tools which can perform various operations on a WordPress installation. One of the greatest examples of this is the WordPress Mobile App.

在本教程中,我们将介绍可以使用XML-RPC远程执行的WordPress核心功能。 这可以帮助我们构建可以在WordPress安装上执行各种操作的工具。 最好的例子之一是WordPress移动应用程序

XML-RPC协议请求和响应概述 (Overview of an XML-RPC Protocol Request and Response)

To make an XML-RPC request you need to wrap the remote function name and parameters in XML format and then send a POST request using HTTP.

要发出XML-RPC请求,您需要以XML格式包装远程函数名称和参数,然后使用HTTP发送POST请求。

This is an example of a XML-RPC HTTP request:

这是XML-RPC HTTP请求的示例:

POST /xmlrpc HTTP 1.0
User-Agent: myXMLRPCClient/1.0
Host: 192.168.124.2
Content-Type: text/xml
Content-Length: 169
<?xml version="1.0"?>
<methodCall>
   <methodName>circleArea</methodName>
      <params>
         <param>
            <value><double>2.41</double></value>
         </param>
      </params>
</methodCall>

Here’s a sample response to the above request:

这是对上述请求的示例响应:

HTTP/1.1 200 OK
Date: Sat, 06 Oct 2001 23:20:04 GMT
Server: Apache.1.3.12 (Unix)
Connection: close
Content-Type: text/xml
Content-Length: 124

<?xml version="1.0"?>
<methodResponse>
   <params>
      <param>
         <value><double>18.24668429131</double></value>
      </param>
   </params>
</methodResponse>

In this tutorial we’ll use PHP to send XML-RPC requests to WordPress and display the raw response.

在本教程中,我们将使用PHP将XML-RPC请求发送到WordPress并显示原始响应。

We would generally write code to wrap our function name and parameters in XML format and then make an HTTP request using cURL, but writing code for this from scratch is lengthy. Instead we can use the PHPXMLRPC library, which provides abstraction to all of these steps and lets us make XML-RPC requests much more easily.

通常,我们将编写代码以XML格式包装函数名称和参数,然后使用cURL发出HTTP请求,但是从头开始为此编写代码很长。 相反,我们可以使用PHPXMLRPC库,该库为所有这些步骤提供了抽象,并使我们可以更轻松地发出XML-RPC请求。

XML-RPC libraries are available for all popular programming languages, you can find one for your preferred language using your favorite search engine.

XML-RPC库可用于所有流行的编程语言,您可以使用自己喜欢的搜索引擎为自己的首选语言找到一种。

WordPress XML-RPC功能 (WordPress XML-RPC Functions)

There are lots of WordPress core functions that WordPress exposes via XML-RPC. All of the XML-RPC exposed functions are categorized into 9 categories: Posts, Taxonomies, Media, Comments, Options, Users, Categories, Tags and Pages.

WordPress通过XML-RPC公开了许多WordPress核心功能。 所有XML-RPC公开的功能都分为9类:帖子,分类法,媒体,注释,选项,用户,类别,标签和页面。

功能列表 (A List of the Functions)

Here’s the complete list of all functions:

这是所有功能的完整列表:

Posts functions: Available from WordPress 3.4. Here is the list of functions that belong to posts category:

帖子功能 :可从WordPress 3.4获得。 这是属于帖子类别的功能列表:

wp.getPost
wp.getPosts
wp.newPost
wp.editPost
wp.deletePost
wp.getPostType
wp.getPostTypes
wp.getPostFormats
wp.getPostStatusList

Taxonomies functions: Available from WordPress 3.4. Here is the list of functions that belong to taxonomies category:

分类法功能 :可从WordPress 3.4获得。 这是属于分类法类别的函数列表:

wp.getTaxonomy
wp.getTaxonomies
wp.getTerm
wp.getTerms
wp.newTerm
wp.editTerm
wp.deleteTerm

Media functions: Available from WordPress 3.1. Here is the list of functions that belong to taxonomies category:

媒体功能 :可从WordPress 3.1获得。 这是属于分类法类别的函数列表:

wp.getMediaItem
wp.getMediaLibrary
wp.uploadFile

Comments functions: Available from WordPress 2.7. Here is the list of functions that belong to the comments category:

注释功能 :可从WordPress 2.7获得。 这是属于注释类别的功能列表:

wp.getCommentCount
wp.getComment
wp.getComments
wp.newComment
wp.editComment
wp.deleteComment
wp.getCommentStatusList

Options functions: Available from WordPress 2.6. Here is the list of functions that belong to the options category:

选项功能 :可从WordPress 2.6获得。 这是属于选项类别的功能列表:

wp.getOptions
wp.setOptions

Users functions: Available from WordPress 3.5. Here is the list of functions that belong to the users category:

用户功能 :可从WordPress 3.5获得。 以下是属于用户类别的功能列表:

wp.getUsersBlogs
wp.getUser 
wp.getUsers 
wp.getProfile 
wp.editProfile
wp.getAuthors

Categories functions: Available from WordPress 3.4. Here is the list of functions that belong to the categories category:

类别功能 :可从WordPress 3.4获得。 以下是属于类别类别的功能列表:

wp.getCategories
wp.suggestCategories
wp.newCategory
wp.deleteCategory

Tags functions: Available from WordPress 3.4. Here is the list of functions that belong to the tags category:

标签功能 :可从WordPress 3.4获得。 以下是属于标签类别的功能列表:

wp.getTags

Pages functions: Available from WordPress 3.4. Here is the list of functions that belong to the pages category:

页面功能 :可从WordPress 3.4获得。 以下是属于页面类别的功能列表:

wp.getPage
wp.getPages
wp.getPageList
wp.newPage
wp.editPage
wp.deletePage
wp.getPageStatusList
wp.getPageTemplates

All of the category names and function names, as well as the use and purposes are quite self explanatory.

所有类别名称和函数名称以及用途和目的都是很容易解释的。

Let’s see some examples of the above functions:

让我们看一些上述功能的例子:

获取WordPress作者列表 (Getting a List of WordPress Authors)

Here is the code to get list of all authors of a remote WordPress installation using PHP:

这是获取使用PHP进行远程WordPress安装的所有作者的列表的代码:

<?php 
    include("lib/xmlrpc.inc");
    $function_name = "wp.getAuthors";
    $url = "https://www.sitepoint.com/xmlrpc.php";
 
    $client = new xmlrpc_client($url);
    $client->return_type = "phpvals";
    $message = new xmlrpcmsg($function_name, array(new xmlrpcval(0, "int"), new xmlrpcval("username", "string"), new xmlrpcval("password", "string")));
    $resp = $client->send($message);
 
    if ($resp->faultCode()) echo 'KO. Error: '.$resp->faultString(); else foreach ($resp->val as $key => $value) {
        echo "User id: " . $value["user_id"];
        echo "<br><br>";
        echo "Username: " . $value["user_login"];
        echo "<br><br>";
        echo "Display name: " . $value["display_name"];
        echo "<br><br>";
    };
?>

Let’s see how the above code works:

让我们看看上面的代码是如何工作的:

  • First we included PHPXMLRPC library.

    首先,我们包含了PHPXMLRPC库。
  • Then we create a variable $function_name to hold the function name.

    然后,我们创建一个变量$function_name来保存函数名称。

  • We created an another variable which points to the xmlrpc.php file of the WordPress installation. This file always exists in the root of WordPress.

    我们创建了另一个变量,该变量指向WordPress安装的xmlrpc.php文件。 此文件始终存在于WordPress的根目录中。

  • Then we create an XML-RPC client object and pass the URL to the constructor.

    然后,我们创建一个XML-RPC客户端对象,并将URL传递给构造函数。
  • We then instruct the library to convert the response data into a PHP array variable so it will be easy to read and work with the response data. Working with raw XML response data will be difficult as we have to parse the XML.

    然后,我们指示该库将响应数据转换为PHP数组变量,这样将很容易读取和使用响应数据。 使用原始XML响应数据将很困难,因为我们必须解析XML。
  • Then we construct a request message object with the parameters for the wp.getAuthors function. First parameters is the blog id, the other two parameters are the username and password of the administrator.

    然后,我们使用wp.getAuthors函数的参数构造一个请求消息对象。 第一个参数是博客ID,其他两个参数是管理员的用户名和密码。

  • Next, we send the XML-RPC request.

    接下来,我们发送XML-RPC请求。
  • Finally we get the response. If there’s an error we display the error, otherwise we loop the response object’s value property to print the authors basic information.

    最后我们得到了回应。 如果有错误,则显示错误,否则,循环响应对象的value属性以打印作者的基本信息。

创建帖子 (Creating a Post)

We just saw how easy it is to retrieve a list of authors, here’s how you can create a post:

我们刚刚看到检索作者列表很容易,这是创建帖子的方法:

<?php 
	include("lib/xmlrpc.inc");
	$function_name = "wp.newPost";
	$url = "https://www.sitepoint.com/xmlrpc.php";

	$client = new xmlrpc_client($url);
	$client->return_type = 'phpvals';

	$message = new xmlrpcmsg(
			$function_name, 
			array(
				new xmlrpcval(0, "int"), 
				new xmlrpcval("my_cool_username", "string"), 
				new xmlrpcval("my_super_secret_password", "string"), 
				new xmlrpcval(
					array(
						"post_type" => new xmlrpcval("post", "string"), 
						"post_status" => new xmlrpcval("draft", "string"), 
						"post_title" => new xmlrpcval("Sitepoint is Awesome", "string"), 
						"post_author" => new xmlrpcval(1, "int"), 
						"post_excerpt" => new xmlrpcval("excerpt", "string"), 
						"post_content" => new xmlrpcval("content", "string")
						), 
					"struct"
					)
				)
			);

	$resp = $client->send($message);

	if ($resp->faultCode()) echo 'KO. Error: '.$resp->faultString(); else echo "Post id is: " . $resp->value();
?>

Here, we called the function wp.newPost. Along with the blog id, username and password. We also passed a struct type containing post type, status, title, content, author and excerpt.

在这里,我们调用了函数wp.newPost 。 以及博客ID,用户名和密码。 我们还传递了一个包含帖子类型,状态,标题,内容,作者和摘录的结构类型。

注意:检测XML-RPC请求 (Note: Detecting the XML-RPC Request)

A quick note: If you’re a plugin or theme developer, then you might want your code to function differently for XML-RPC requests. WordPress allows a way for themes and plugin to detect if WordPress is processing a XML-RPC request.

快速说明:如果您是插件或主题开发人员,则可能希望代码对XML-RPC请求的功能有所不同。 WordPress提供了一种主题和插件检测WordPress是否正在处理XML-RPC请求的方法。

Here is the code to detect XML-RPC requests:

这是检测XML-RPC请求的代码:

if(defined('XMLRPC_REQUEST')) 
{ 
	// XML-RPC request
} 
else
{
	// Normal HTTP request
}

结论 (Conclusion)

In this article we covered the fundamentals of XML-RPC for WordPress, including the basics of XML-RPC and how WordPress exposes this protocol. We also demonstrated how to perform various operations on a WordPress installation using XML-RPC. You can now create a mobile, desktop or a web application XML-RPC client for WordPress.

在本文中,我们介绍了WordPress的XML-RPC的基础知识,包括XML-RPC的基础知识以及WordPress如何公开此协议。 我们还演示了如何使用XML-RPC在WordPress安装上执行各种操作。 现在,您可以为WordPress创建移动,桌面或Web应用程序XML-RPC客户端。

翻译自: https://www.sitepoint.com/xml-rpc-for-wordpress-developers/

wordpress本地开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值