繁体   English   中英

使用google.com/tbproxy/spell进行拼写检查

[英]spell check using google.com/tbproxy/spell

我在我的项目中使用Google拼写检查并按照博客中的建议执行所有操作,但它似乎对我不起作用。 你能看一下这个并告诉我我做错了什么。

在我的JavaScript中:

function makeRequest(parameters, svalue) {  
  console.log("dv-- inside makerequest 1");
  console.log("svalue =", svalue);
  http_request.onreadystatechange = GetResponse;
  http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en', true);    

  data = '<?xml version="1.0" encoding="utf-8" ?>';
  data +='<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>';
  data += svalue;
  data += '</text></spellrequest>';
  console.log("data =", data)
  http_request.send(data);
}

function GetResponse(){
  console.log("dv-- inside GetResponse-1 http_request.readyState =", http_request.readyState)
  if (http_request.readyState == 4) {
console.log("dv-- inside GetResponse-2 http_request.status =", http_request.status)
    if (http_request.status == 200) {
  http_response = http_request.responseText;
  console.log("dv --http_response =", http_response)
    }
    else {
      console.log('There was a problem with the request' + '& http_request.status =' + http_request.status );
    }
  }
}

我的PHP代码:spellify.php

$url="http://www.google.com/tbproxy/spell?lang=en&hl=en";
//$data = file_get_contents('php://input');
$data = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hellow  </text></spellrequest>';

$data = urldecode($data);

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch); 

print $contents;
?>

我尝试在我的PHP中硬编码数据,以确保数据正确传递,这不是一个问题。

Firefox给出错误: Error: unclosed token源文件: http://mysite.com/Project/spellify.php?lang=en Error: unclosed token ?xml version="1.0" encoding="utf-8" ?>< http://mysite.com/Project/spellify.php?lang=en行:5,列:183 Source Code: $ data = '< ?xml version="1.0" encoding="utf-8" ?>< spellrequest textalreadyclipped =”0“ignoredups =”0“ignoredigits =”0“ignoreallcaps =”0“> < text>hello thsi is graet< / text> <`/ spellrequest>' ;

Chrome不会出现任何错误,但会在控制台中显示如下:

dv-- inside makerequest 1
spellify.js:419svalue = hello worlda 
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 1
spellify.js:427data = <?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello worlda </text></spellrequest>
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 2
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 3
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 4
spellify.js:435dv-- inside GetResponse-2 http_request.status = 200
spellify.js:438dv --http_response = <?php

$url="http://www.google.com/tbproxy/spell?lang=en&hl=en";
// $data = file_get_contents('php://input');
$data = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello thsi is graet</text></spellrequest>';
$data = urldecode($data);

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
$contents = curl_exec ($ch);
curl_close ($ch); 

print $contents;
?>

请建议可以纠正哪些方法来完成这项工作。

谢谢

您正在使用来自spellify.com的代码,对原始开发人员的某种信誉表示赞赏。

1)您错误地修改了spellify.php文件。 2-a)Spellify基本上准备http请求并通过Javascript src / spellify.js解析http响应,所以它在spellify.php中进行任何更改是错误的,而你应该检查你是否引用了spellify / src / scriptaculous.js和spellify / src / prototype.js正确。

2-b)在相同的spellify.js下你有不正确的硬编码跟随线;

http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en', true);

它应该如下;

http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en'+parameters, true);

3)编写spellify.php的时间,SSL验证可能没有问题,但现在它不会返回任何内容,除非您忽略SSL验证,在$ content = curl_exec($ ch)之前在spellify.php中添加以下行;

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

这是示例实现。

http://amitgandhi.in/2013/01/25/spell-check-utility-using-jquery-and-google-api/

  • 你可以添加单词字典。 变量$.SpellChecker.dicWords是一个维护它的javascript数组。 您可以使用数据库表将此数组填充为现有单词。 目前这个应用程序没有连接到数据库,所以如果你添加你的单词,那么单词的生命周期只会在页面重新加载之前。

  • 使用google.com/tbproxy/spell api调用(用PHP编写)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM