我正在尝试使用PHP curl实现以下流表达式。
curl --data-urlencode 'expr=search(enron_emails,
q="from:1800flowers*",
fl="from, to",
sort="from asc",
qt="/export")' http://localhost:8983/solr/enron_emails/stream
在命令行上执行curl请求可以正常工作。但是,使用php curl运行相同文件将不起作用。我使用了以下PHP代码。
function streamSolrData() {
$url = 'http://localhost:8983/solr/enron_emails/stream';
$data['expr']['search'] = "enron_emails";
$data['expr']['search']['q'] = 'from:1800flowers*';
$data['expr']['search']['fl'] = "from, to";
$data['expr']['search']['sort'] = "from asc";
$data['expr']['search']['qt'] = "/export";
$data = json_encode($data);
$ch = curl_init();
if ($ch) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$response = curl_exec($ch);
}
}
以上查询仅返回{"result-set":{"docs":[{"EXCEPTION":null,"EOF":true}]}}
,没有任何结果。
知道我在这里缺少什么吗?
我找到了答案。理想情况下,solr希望传递称为expr的单个参数。以下是解决方案。
function streamSolrData() {
$url = 'http://localhost:8983/solr/enron_emails/stream';
$data['expr'] = 'search(enron_emails,
q="from:1800flowers*",
fl="from, to",
sort="from asc",
qt="/export")'
$ch = curl_init();
if ($ch) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
}
}
答案 0 :(得分:0)
您正在发送JSON,但为流表达式提供的结构不是JSON,因此将内容编码为JSON将发出无效请求。
如果您查看查询:
expr=search(enron_emails,
q="from:1800flowers*",
fl="from, to",
sort="from asc",
qt="/export")
..无论如何,这不是有效的JSON对象或JSON结构。
相反,请使用CURLOPT_POSTFIELDS
,其中包括POST请求正文中未处理的内容:
curl_setopt($ch, CURLOPT_POSTFIELDS,
urlencode('expr=search(enron_emails,
q="from:1800flowers*",
fl="from, to",
sort="from asc",
qt="/export")'));