为wordpress编写外部api时出错?

时间:2018-08-23 10:46:43

标签: php wordpress api

你好

我正在为我的wordpress网站编写一个外部api。 我的代码包括三个文件:

文件01:Connect.php

class Connect{

    private $con ;
    public function __construct()
    {



    }

    public function ConnectDb()
    {
        $this->con = mysqli_connect("localhost","root","","my_mydb");
        mysqli_set_charset($this->con,"utf8");
        if (!$this->con)
        {
            die("Connection error: " . mysqli_connect_error());
        }
        return $this->con;
    }

}

文件02:DbOperations.php

include_once ('Connect.php');
require_once ('../../wp-load.php');
class DbOperations{

    private $con ;
    public function __construct()
    {
        $dbCon = new Connect();
        $this->con = $dbCon->ConnectDb();
    }


    public function get_all_posts()
    {
        $query = "SELECT post_author,post_date,post_content,post_title,post_name,comment_count FROM wp_users ";
        $result = mysqli_query($this->con,$query,MYSQLI_USE_RESULT);
        $num = mysqli_num_rows($result);
        $allposts = array();
        if (!$num==0)
        {
            while ($row=mysqli_fetch_array($result))
            {
                $allposts['post_title'] = $row['post_title'];
                $allposts['post_author'] = $row['post_author'];
                $allposts['post_date'] = $row['post_date'];
                $allposts['post_content'] = $row['post_content'];
                $allposts['post_name'] = $row['post_name'];
                $allposts['comment_count'] = $row['comment_count'];
            }
            $allposts['error'] = false ;
            $allposts['message'] = "Succeed";


        }
        else
        {
            $allposts['error'] = true ;
            $allposts['message'] = "cant get all posts";
        }
        return $allposts;

    }


    public function isUserLogined($username,$password)
    {
        $query = "SELECT user_login,user_pass FROM wp_users WHERE user_login='".$username."'";
        $result = mysqli_query($this->con,$query);
        $nums = mysqli_num_rows($result);
        if (!$nums==0)
        {
            $row = mysqli_fetch_assoc($result);
            $user_login = $row['user_login'];
            $user_pass = $row['user_pass'];
            if (wp_check_password($password,$user_pass))
            {
                $loginResponse['error'] = false ;
                $loginResponse['message'] = "Succees mysqli_fetch_assoc Error";
                $loginResponse['user_login'] = $user_login;
                $loginResponse['user_pass'] = $user_pass;

            }
            else
            {
                $loginResponse['error'] = true ;
                $loginResponse['message'] = "wp_chech_password and mysqli_fetch_assoc Error";
                $loginResponse['user_login'] = "";
                $loginResponse['user_pass'] = "";
            }
        }
        else
        {
            $loginResponse['error'] = true ;
            $loginResponse['message'] = "Can not find user,Query Error in isUserLogined";
            $loginResponse['user_login'] = "";
            $loginResponse['user_pass'] = "";
        }

        return $loginResponse;


    }

}

文件03:myApi.php

include ('DbOperations.php');

$dbOperate = new DbOperations();

$resultArray = $dbOperate->get_all_posts();

echo json_encode($resultArray);

我将这三个文件放入目录: C:\ xampp \ htdocs \ wp-includes \ rest-api \

,访问它的地址是: http://localhost/wp-includes/rest-api/myApi.php

但是当我在地址栏中输入此内容时,我得到以下结果:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\wp-includes\rest-api\DbOperations.php on line 25
{"error":true,"message":"cant get all posts"}

我在哪里错? 我搜索但什么都没找到

1 个答案:

答案 0 :(得分:0)

尝试使用

if (!$num|| mysqli_num_rows($num) == 0){

代替

if (!$num==0){

还将$ result中的MYSQLI_USE_RESULT删除