Php JSON post,从webservice以格式化字符串返回答案
JSON post,它从webservice以格式化字符串返回答案(例如,我有一个带有搜索框的网站,无论我在搜索框中键入什么,我都想要一个格式化字符串,它会给我一个未格式化的答案转储)Php JSON post,从webservice以格式化字符串返回答案,php,html,ajax,json,curl,Php,Html,Ajax,Json,Curl,JSON post,它从webservice以格式化字符串返回答案(例如,我有一个带有搜索框的网站,无论我在搜索框中键入什么,我都想要一个格式化字符串,它会给我一个未格式化的答案转储) [1] => stdClass Object ( [value] => FOCUSLESS ) )
[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"
以下是带有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>
[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"
我真正想要的是字符串返回(实现社区护理BLAHBLAHBLAH………等等)
[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"
我改进了代码并注释掉了$extract,这让我很困惑。我还将echo代码更改为print_r(json_decode($result))
[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"
运行时提取:
{"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
stdClass Object
[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"
(
[问题]=>stdClass对象
(
[qclasslist]=>数组
(
[0]=>stdClass对象
(
[值]=>描述性
)
[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"
我离我的目标越来越近了,只需要在提取[标题]和[文本]方面得到帮助字段,这只是一个简单的问答概念。非常感谢您的帮助。我听说格式化的答案会有帮助,但不确定我是否正确声明了它。很难判断它是对象还是数组,但请尝试将
替换为
或
[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"
如果它们不起作用,请尝试将其替换为:
并发布结果
[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"
编辑
[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"
试试这个:但是很难看到物体的结构。。。
你只需要给我看一个格式正确的对象,我就可以给出明确的答案
[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"
$result->question->evidencelist[0]->text
我试过print_r(json_decode($result));它工作得更好,因为你可以看到上面的结果,但仍然没有给我一个字段“title”和“text”。这是一个问答项目,请不要使用json解码,只需发布print_r($result)的结果.json_decode是用于javascript的,我不知道你为什么要使用它。我得到了我第一次得到的东西,json_decode在我使用它的时候就构建了它。谢谢你,伙计,我会继续努力把这段代码弄好的。嘿,Christian,因为我是第一次这样做,所以我想要实现的就是用一个问题连接到一个Web服务并得到一个结果这在文本字段中显示了答案。我正在使用json,但仍在编写代码。有人建议我使用ajax帮助提取答案。这是我的目标,感谢您迄今为止的帮助