在wordpress中清理json输出

时间:2018-05-27 16:39:51

标签: php json wordpress

我在wordpress(最新版本)中使用钩子作为插件自定义代码。我在数据库中也有一些自定义表。我从数据库查询一些数据并使用JSON输出数组。输出很好,但由于它基于钩子,输出带有一个完整的页面(html标题和正文)。我需要的是只输出JSON代码,以便我的移动应用程序可以理解代码。

这是一个例子:

我的钩子代码:

    <?php

    $getPosts = $wpdb->get_results("SELECT * FROM gc_posts WHERE 
    userid=$organization");
    foreach($getPosts as $getPosts){
    $imageLocation = $target_dir . $getPosts->image;

    $myContents->title = "$getPosts->title";
    $myContents->description = "$getPosts->description";
    $myContents->image = "$imageLocation";


}
wp_send_json($myContents);

这是当前的输出:

<!DOCTYPE html>
<html lang="en-US" class="no-js no-svg">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<script>(func
...
<body>
...
{"title":"Museum","description":"Open from 10 am to 10 pm","image":"1_1527407746.jpg"}
...

虽然我正在寻找的只是JSON代码,如:

{"title":"Museum","description":"Open from 10 am to 10 pm","image":"1_1527407746.jpg"}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先,感谢洋红色回答这个问题。

我按照magenta answe而不是使用正常的帖子页网址,我使用了以下内容:

2018-05-28 12:03:40 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array()
2018-05-28 12:03:40 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed [/home/beesystems/public_html/specs/vendor/phpmailer/phpmailer/src/SMTP.php line 325]
2018-05-28 12:03:40 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [/home/beesystems/public_html/specs/vendor/phpmailer/phpmailer/src/SMTP.php line 325]
2018-05-28 12:03:40 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [/home/beesystems/public_html/specs/vendor/phpmailer/phpmailer/src/SMTP.php line 325]
2018-05-28 12:03:40 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我在我的插件中添加了一个动作,所以这是允许的:

            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'ssl';
            $mail->Host = "smtp.gmail.com";
            $mail->Port = 465;

           $mail->Username = \Yii::$app->params['mailer-account'];
           $mail->Password = \Yii::$app->params['mailer-password'];