JSON帖子从webservice返回格式化字符串中的答案

时间:2015-08-04 09:04:04

标签: php html ajax json curl

JSON帖子从webservice返回格式化字符串中的答案(例如,我有一个带搜索框的网站,无论我在搜索框中输入什么,我想要一个格式化的字符串,而不是它给我一个答案的转储,这是没有格式化)

以下是带有JSON的HTML / PHP代码:

 <!-- Start of Search Wrapper -->
            <div class = "imageContainer">
            <a href="#"><img src="images/" title="logo" /></a>
            </div>
            <div class="search-area-wrapper">
                    <div class="search-area container">

                            <h3 class="search-header">Have a Question?</h3>
                            <p class="search-tag-line">If you have any question you can ask below or enter what you are looking for!</p>

                            <form id="search-form" class="search-form clearfix" method="get" action="" autocomplete="off">
                                    <input class="search-term required" type="text" id="questionText" name="questionText" placeholder="What is non medical prescribing?"
                                    title="* Please enter a search term!" />
                                    <input class="search-btn" type="submit" value="Search" />
                                    <div id="search-error-container"></div>
                            </form>
                    </div>
            </div>

                            <?php

                                    header('Content-type: application/json');
                                    header("Content-Type:application/json");

                                    // Credentials login details and URL connect
                                    $username = "";
                                    $password = "";
                                    $question = $_REQUEST['questionText'];
                                    $url = "https://test.com";
                                    $auth = base64_encode($username . ":" . $password);
                                    //askQuestion();

                       //     ------------------------------------------------------------------------------------------------------------- //

                            try {

                                   $data = json_encode(array(

                                   'question' => array(
                                   'evidenceRequest' => array('items' => 1),
                                       'formattedAnswer' => true,
                                   'questionText' => $question,

                                   ))
                                  );
                                 //$Extract = json_decode($data,true);
                                 // echo $data;
                                  //  echo $result;

                                  $curl = curl_init();

                                  curl_setopt($curl, CURLOPT_HTTPHEADER, array(

                                      'Content-Type: application/json',
                                      'Accept: application/json',
                                      'X-synctimeout: 30',
                                      'Authorization: Basic ' . $auth,
                                      'Content-Length: ' . strlen($data))
                                  );

                                    curl_setopt($curl, CURLOPT_POST, true);
                                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                                    curl_setopt($curl, CURLOPT_URL, $url);
                                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                                    $result = curl_exec($curl);


                                } catch(Exception $e) {
                                              echo '<p>There Was an Error Accessing the Webservice!!!</p>';
                                              echo $e->getMessage();
                                            }

                                    ?>




                    <!-- End of Search Wrapper -->

    <!-- Start of Page Container -->

            <div class="page-container">
                    <div class="container">

                            <!-- start of page content -->
                            <div class="span8 page-content">

                                            <!-- Basic Home Page Template -->

                        <div class="widget-area no-padding blank">
                            <div class="status-upload">
                                <form>
                                      <textarea class='mail' name="body" cols="50" rows="5" align="left">

                                      <?print_r(json_decode($result));?>

                                     </textarea>

                                </form>
                            </div><!-- Status Upload  -->
                        </div></div></div></div>

运行时提取:

 {"question":{"qclasslist":[{"value":"DESCRIPTIVE"},{"value":"FOCUSLESS"},{"value":"DEFINITION"}],"focuslist":[{"value":"brent"}],"latlist":[{"value":"brent"}],"evidencelist":[{},{"value":"0.04949565976858139","text":" To achieve Community Care BLAHBLAHBLAH.........ETC

我真正想要的是字符串返回(实现社区护理BLAHBLAHBLAH ......... ETC)

我改进了代码并注释掉了$ extract,让我很困惑。我还将echo代码更改为print_r(json_decode($ result));

运行时提取:

 stdClass Object

(     [问题] =&gt; stdClass对象         (             [qclasslist] =&gt;排列                 (                     [0] =&gt; stdClass对象                         (                             [value] =&gt;描述性                         )

                [1] => stdClass Object
                    (
                        [value] => FOCUSLESS
                    )

            )

        [evidencelist] => Array
            (
                [0] => stdClass Object
                    (
                        [value] => 0.48106712102890015
                        [text] => BLAH BLAH BLAH BLAH
                        [id] => 001
                        [title] => What is non-medical prescribing?
                        [document] => Post/question/djncjabuvas
                        [copyright] => "URL"
                        [termsOfUse] => "URL"

我越来越接近我的目标,只需要提取[Title]和[text]字段,这只是一个简单的问答概念。非常感谢您的帮助。我听说格式化答案会有所帮助,但不确定我是否正确宣布它。

1 个答案:

答案 0 :(得分:0)

很难判断它是对象还是数组,但尝试将<?php echo json_encode($result, true)?>替换为<?php echo $result->text; ?><?php echo $result['text']; ?>

如果它们不起作用,请尝试将其替换为:<?php var_dump($result); ?>并发布结果。

修改

试试这个:但很难看到对象的结构...... 你真的只需要向我展示一个格式正确的对象,我可以给出明确的答案。

$result->question->evidencelist[0]->text