块引用
我有一个 Web 应用程序(用 js 编写),用户可以在其中输入名称(以及其他条件)并执行对 ML 数据库的搜索,并以表格表示与该名称(或其他搜索条件)关联的一些数据) 被退回。在开发的第一阶段,我编写了 optic api 查询,并将 name 的值硬编码在查询的 where 语句中。我这样做是为了测试 REST api 是否正确调用了查询等。一切正常,我可以看到在 Web 应用程序中返回的硬编码名称的正确结果。现在我想将用户提供的 name 值传递给 Optic api 查询 (EntityInformation_Optic_API_Query.json) 以替换 where 语句中的硬编码值。我该怎么做呢?提前致谢。
所有者编辑
该应用程序是用 PHP 编写的,我在下面复制了代码。在此您可以看到调用 optic api 查询的位置以及构建参数的位置。
<?php
if (!defined('RSS_BASE_URL')) exit;
use MarkLogic\MLPHP as MLPHP;
function rss_api_call($json, $params = array(), $resource = 'rows', $verb = 'POST')
{
$json = __DIR__ . '/json/' . $json;
if (!file_exists($json)) return false;
$request = new MLPHP\RESTRequest('POST', 'rows', $params, file_get_contents($json), array('Content-type' => 'application/json'));
$response = rss_get_client()->send($request);
return json_decode($response->getBody());
}
function rss_api_entities()
{
$params = array();
return rss_api_call('EntityInformation_Optic_API_Query.json', $params);
//return (empty($params)) ? '' : rss_api_call('EntityInformation_Optic_API_Query.json', $params);
}
function rss_api_search()
{
$params = array();
if (isset($_POST['companyname']) && !empty($_POST['companyname']))
{
$params['CompanyName'] = htmlspecialchars($_POST['companyname']);
}
if (isset($_POST['EIN']) && !empty($_POST['ein']))
{
$params['EIN'] = htmlspecialchars($_POST['ein']);
}
if (isset($_POST['city']) && !empty($_POST['city']))
{
$params['EntityCity'] = htmlspecialchars($_POST['city']);
}
if (isset($_POST['state']) && !empty($_POST['state']))
{
$params['EntityState'] = htmlspecialchars($_POST['state']);
}
if (isset($_POST['zip']) && !empty($_POST['zip']))
{
$params['EntityZip'] = htmlspecialchars($_POST['zip']);
}
return rss_api_call('SearchResults_Optic_API_Query.json', $params);
//return (empty($params)) ? '' : rss_api_call('SearchResults_Optic_API_Query.json', $params);
}
function rss_asset_path($path, $file_name)
{
global $rss_manifest;
if (empty($rss_manifest))
{
ob_start();
include(__DIR__ . '/../manifest.json');
$rss_manifest = json_decode(ob_get_clean(), true);
}
if (isset($rss_manifest[$file_name]))
{
$file_name = $rss_manifest[$file_name];
}
return RSS_ASSET_PATH . $path . '/' . $file_name;
}
function rss_box($title, $content)
{
echo '<div class="rss-box">' .
'<div class="rss-box-title">' . $title . '</div>' .
'<div class="rss-box-content">' . $content . '</div>' .
'</div>';
}
function rss_get_client()
{
$mlphp = new MLPHP\MLPHP(array
(
'host' => RSS_API_HOST,
'port' => RSS_API_PORT,
'version' => RSS_API_VERSION,
'username' => RSS_API_USERNAME,
'password' => RSS_API_PASSWORD
));
return $mlphp->getClient();
}
function rss_hidden_search_fields()
{
echo '<div class="rss-hidden">';
$fields = array('debug', 'companyname', 'ein', 'city', 'state', 'zip');
foreach ($fields as $field)
{
echo (isset($_POST[$field])) ? '<input name="' . $field . '" type="hidden" value="' . htmlspecialchars($_POST[$field]) . '" />' : '';
}
if (!empty($_POST['social']))
{
$social = (is_array($_POST['social'])) ? $_POST['social'] : array($_POST['social']);
foreach ($social as $social_network)
{
echo '<input name="social[]" type="hidden" value="' . htmlspecialchars($social_network) . '" />';
}
}
echo (empty($_POST['social-select-all'])) ? '' : '<input name="social-select-all" type="hidden" value="1" />';
echo '</div>';
}
Part of the EntityInformation_Optic_API_Query.json is below
-----
edit (information from a comment below):
The request will be a POST to /v1/rows. Does the following payload look correct?
```javascript
{
"$optic": {
"ns": "op",
"fn": "operators",
"args": [
{
"ns": "op",
"fn": "from-view",
"args": [ "TestSchema", "SUT", null, null ]
},
{
"ns": "op",
"fn": "where",
"args": [
{
"ns": "op",
"fn": "eq",
"args": [
{
"ns": "op",
"fn": "col",
"args": [ "CompanyName" ]
},
"${req.params.CompanyName}"
]
}
]
}