开发者

spell check using google.com/tbproxy/spell

开发者 https://www.devze.com 2023-04-03 21:56 出处:网络
I am using google spell check in my project and doing everything as suggested in the blogs but it doesn\'t seem to work for me. Can you please look at this and tell me what I am doing incorrect.

I am using google spell check in my project and doing everything as suggested in the blogs but it doesn't seem to work for me. Can you please look at this and tell me what I am doing incorrect.

In my 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 );
    }
  }
}

My PHP Code: 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;
?>

I tried hardcoding the data in my php to make sure data was passed correctly and that is not an issue.

Firefox give an error: Error: unclosed token Source File: http://mysite.com/Project/spellify.php?lang=en Line: 5, Column: 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 doesn't give any error, but show below in console:

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" ?><spellrequ开发者_JS百科est 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;
?>

Please suggest what could be corrected to make this work.

Thanks


You are using code from spellify.com, some sort of credit to original developer is appreciated.

1) You have incorrectly modified spellify.php file. 2-a) Spellify basically prepare http-request and parse http-response via Javascript src/spellify.js, so its incorrect to make any changes in spellify.php, rather you should check if you are referencing spellify/src/scriptaculous.js and spellify/src/prototype.js correctly.

2-b) Under same spellify.js you have hardcoded following line incorrectly;

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

It should be as follows;

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

3) The time when spellify.php was written, there might have no issue with SSL verification, but now it wont return anything unless you ignore the SSL Verification, add following lines in spellify.php, before $contents = curl_exec ($ch);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


Here is sample implementation.

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

  • You can add words dictionary. Variable $.SpellChecker.dicWords is a javascript array which maintain this. You can use your database table populate this array with existing words. Currently this app is not connected to database so if you add your words then lifetime of words would only till page reloads.

  • Uses google.com/tbproxy/spell api call (Written in PHP)

0

精彩评论

暂无评论...
验证码 换一张
取 消