今天WordPress主题站简单介绍一下WordPress系统中用户信息获取方式,今天就讲讲使用接口方式获取WordPress用户信息的方法。
接口文件如下:
<?php
if (\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’POST\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ != $_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’REQUEST_METHOD\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) {
header(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’Allow: POST\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);
header(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’HTTP/1.1 405 Method Not Allowed\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);
header(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’Content-Type: text/plain\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);
exit;
}
require \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’wp-load.php\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;
$checkpwd = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”cscdaimadog\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;
$action = isset($_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’action\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) ? (int) $_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’action\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] : 0;
$pwd = isset($_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’pwd\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) ? $_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’pwd\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] : null;
if ($pwd != $checkpwd) {
print_r(json_encode(array(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”code\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” => 100, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”msg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”pwd error\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”)));
exit;
}
switch ($action) {
case 0:
$useroffset = isset($_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’offset\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) ? (int) $_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’offset\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] : 0;
getuser($useroffset);
break;
case 1:
get_count();
break;
}
function getuser($useroffset)
{
$users_args = array(
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’orderby\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’user_registered\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’offset\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => $useroffset,
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’number\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => 1,
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’count_total\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => true,
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’fields\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => array(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’Display_name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’user_email\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’),
);
$blogusers = get_users($users_args);
echo json_encode($blogusers[0]);
}
function get_count()
{
global $wpdb;
$users = $wpdb->get_var(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”select count(id) from $wpdb->users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”);
echo $users;
}
接口详情
仅post方式提交数据有效。并且设置了简单的接口密码验证,每次请求都需要提交密码,否则将无法使用。
总共提供了两个方法,一个用来获取用户总数,以便我们桌面程序调用。另一个用来获取用户信息,这里我们只需要返回用户的名称和邮箱地址。