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

时间:2011-09-08 02:04:01

标签: php javascript ruby-on-rails spell-checking

我在我的项目中使用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 行:5,列:183     Source Code: $ data ='&lt; ?xml version="1.0" encoding="utf-8" ?>< spellrequest textalreadyclipped =“0”ignoredups =“0”ignoredigits =“0”ignoreallcaps =“0”&gt;&lt; text>hello thsi is graet< /文本&GT;&LT;`/ spellrequest&GT;';

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;
?>

请建议可以纠正的内容。

由于

2 个答案:

答案 0 :(得分:2)

您正在使用来自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之前在spellify.php中添加以下行: $ CH);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

答案 1 :(得分:0)

以下是示例实现。

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

  • 您可以添加单词词典。变量$.SpellChecker.dicWords是一个 保持这个的javascript数组。您可以使用数据库表 用现有单词填充此数组。目前这个应用程序不是 连接到数据库,所以如果你添加你的话,然后生活的话 只会在页面重新加载之前

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