mardi 4 août 2015

JSON post which returns answer in formatted string from webservice

JSON post which returns answer in formatted string from webservice (for example, i have a website with a search box, whatever i type in the search box, i want a formatted string instead it gives me a dump of the answer which is not formatted)

Here is the HTML/PHP code with JSON:

 <!-- 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>

Extraction when run:

 {"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

What i really want, is the string return (To achieve Community Care BLAHBLAHBLAH.........ETC)

I have improved the code and commented out $extract, confused me. i also changed the echo code into print_r(json_decode($result));

Extraction when run:

 stdClass Object

( [question] => stdClass Object ( [qclasslist] => Array ( [0] => stdClass Object ( [value] => DESCRIPTIVE )

                [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"

I am getting closer to my goal and just need help on just extracting the [Title] and [text] field, this is just a simple question and answer concept. Your help is greatly appreciated. I heard formattedAnswer would help but unsure if i declared it correctly.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire