Spell Check in PHP with Yahoo API


by Php Ninza on July 28, 2009

Spell Check is a very nifty feature that a developer need to know , Especially when creating htaccess rewrite rules dynamically you will not want your URL’s to have index with wrong spellings , As this will hamper your SEO performance.

Here is a very easy tool that a developer can implement in their applications for checking the spellings, I have used this API when the users are searching on my web app !, This search make SEO URL’s which i don’t want to index in a incorrect manner.



For using Yahoo SpellCheck API first you need to register a Application ID ! This Application ID will be used to monitor your API calls .

We will be simply making a function here named spell_check which will check the spellings wherever you require.

Let’s create a file name spellcheck.php and put the code given below in that file.

< ?php
function spell_check ($query)
{
// Substitute this application ID with your own application ID provided by Yahoo!.
$appID = "Your App Id here";

// URI used for making REST call. Each Web Service uses a unique URL.
$request = "http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion?appid=$appID&query=".urlencode($query);

// Initialize the session by passing the request as a parameter
$session = curl_init($request);

// Set curl options by passing session and flags
// CURLOPT_HEADER allows us to receive the HTTP header
curl_setopt($session, CURLOPT_HEADER, true);

// CURLOPT_RETURNTRANSFER will return the response
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Make the request
$response = curl_exec($session);

// Close the curl session
curl_close($session);

// Get the XML from the response, bypassing the header
if (!($xml = strstr($response, '<?xml'))) {
        $xml = null;
}

// Create a SimpleXML object with XML response
$simple_xml = simplexml_load_string($xml);

// Traverse XML tree and save desired values from child nodes
	$data = $simple_xml->Result;
	return $data;
}
?>

Above code uses Curl to fetch the API url contents and return the correct spelling , Please change $appID variable with your yahoo App id key.

Once you have created spellcheck.php, Just call this file in a php page and use spell_check function to parse the wrong spellings and correct the data.

Example given below

include ("spellcheck.php");
$correct_spelling = spell_check($wrong_spelling);
echo $correct_spelling;
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 11 comments… read them below or Shout @ me! }

phpcollection July 28, 2009 at 6:29 am

New blog post: Spell Check in PHP with Yahoo API http://bit.ly/i3h3a

This comment was originally posted on Twitter

phpizer July 29, 2009 at 9:12 am

Spell Check using Yahoo API in PHP | PHP tutorials and Scripts Collection http://bit.ly/13aeMf

This comment was originally posted on Twitter

web2feed July 29, 2009 at 10:34 am

Spell Check using Yahoo API in PHP | PHP tutorials and Scripts Collection http://bit.ly/14oEPC #php

This comment was originally posted on Twitter

metoikos July 29, 2009 at 12:32 pm

Spell Check using Yahoo API in PHP | PHP tutorials and Scripts Collection http://ff.im/-5SV4s

This comment was originally posted on Twitter

James Womack July 29, 2009 at 10:33 pm

Thanks for this. I believe that last three lines of spellcheck.php should be something like this:
$xml = strstr($response, ‘<?xml');
return $xml;
}

Php Ninza July 30, 2009 at 2:09 am

@James

Not exactly, Fixed the code above for incorrect information!, Problem was a html < starting tag which was not displaying on post !, Right now replaced it with <

Hope it helps !, Keep in touch !

_Programacion August 19, 2009 at 9:24 am

Comprobar las faltas de ortografĂ­a con PHP http://bit.ly/i3h3a

This comment was originally posted on Twitter

kodegeek August 22, 2009 at 2:47 am

good post! What about google spell check api? i heard that s better than yahoo spell check api.

Php Ninza August 22, 2009 at 4:39 am

@kodegeek

Google spell check api works on google search “I am lucky” feature !, It’s a hack rather than a API.

Yahoo spell check api integrate in a much better way than Google spell check API

sapna December 22, 2009 at 6:39 am

Hi All,

Does yahoo api works for french language.

Thanks

Php Ninza December 22, 2009 at 8:40 am

It does , Just use lang=fr in the API requests

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: