我最近将Sphider爬虫添加到我的网站以添加搜索功能。但是我下载的 Sphider 发行版附带的默认 search.php 太简单了,不能很好地与我网站的其他部分集成。我在网站顶部有一个小导航栏,里面有一个搜索框,我希望能够使用 Ajax 通过该搜索字段访问 Sphider 的搜索结果。为此,我认为我需要让 Sphider 以 JSON 格式返回其结果。
我这样做的方式是使用输出 JSON 的“主题”(Sphider 支持“主题化”它的输出)。我在 Sphider 网站上的这个主题上找到了这个主题。它似乎有效,但更严格的 JSON 解析器不会解析它。这是一些示例 JSON 输出:
{"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ", "results":[ { "idented":"false", "num":"1", "weight":"[100.00%]", "link":"http://www.avtainsys.com/articles/Triple_Contraints", "title":"Triple Contraints", "description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or", "link2":"http://www.avtainsys.com/articles/Triple_Contraints", "size":"3.3kb" }, { "num":"-1" } ], "other_pages":[ { "title":"1", "link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=", "active":"true" }, ] }
问题是结尾附近有一个逗号。据此,在json_decode()
使用 PHP 的函数时“不允许使用尾随逗号” 。此 JSON 也无法使用此在线格式化程序进行解析。但是当我把逗号去掉时,它起作用了,我得到了这个格式更好的 JSON:
{
"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ",
"results":[
{
"idented":"false",
"num":"1",
"weight":"[100.00%]",
"link":"http://www.avtainsys.com/articles/Triple_Contraints",
"title":"Triple Contraints",
"description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or",
"link2":"http://www.avtainsys.com/articles/Triple_Contraints",
"size":"3.3kb"
},
{
"num":"-1"
}
],
"other_pages":[
{
"title":"1",
"link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=",
"active":"true"
}
]
}
现在,我将如何以编程方式执行此操作?而且(也许更重要的是),有没有更优雅的方式来实现这一点?而且您应该知道 PHP 是我可以在我的共享主机帐户上运行的唯一语言,因此例如 Java 解决方案不适合我。