OpenSearch for Yii API

(Gosha Saveiko) #1

I wrote Yii API Open Search plug-in for Firefox and (as I knew later) for IE.

Here are screenshots:

in IE:

in FF:

But Yii site not provides results for OpenSearch in proper format.

That`s why I wrote interfase for Yii suggester.



header("Content-Type: text/html; charset=utf-8");

function make_response($content)


    $title = $desc = $url = array();

    $qa = explode("\n",$content);

    foreach($qa as $value)


        $exploded = array();

        $title[] = $value;

        $exploded = explode('.',$value);

        $desc[] = $exploded[0];

        $str = "";

        $str = ''.$exploded[0];

        if(count($exploded) == 2)


            $str .= '#'.trim($exploded[1],'()').'-detail';


        $url[] = $str;


    return '["'.$_GET['q'].'",["'.implode('","',$title).'"],["'.implode('","',$desc).'"],["'.implode('","',$url).'"]]';




    $link = "".urlencode($_GET['q']).'&limit=100&timestamp='.time();

	$ch = curl_init ();

	curl_setopt ($ch , CURLOPT_URL , $link);

	curl_setopt ($ch , CURLOPT_RETURNTRANSFER , 1 );

	$content = curl_exec($ch);

    echo make_response($content);




Temporary hosted on another site and you can see output here:

Can you help me with host this suggester, or make special suggester page in yii official site to make it more stable.

And the second wish is to redirect from search page if it is the only result for query.


Because of in IE plugin works correctly and goes to defined page straightly. But in FF searcer can`t do it, and redirects to search page.

OPENSEARCH suggestions specification:

P.S.> Sorry for long post and my english.

(Qiang Xue) #2

This is great. Could you describe what format of the response you are expecting? It would be better if you could illustrate it with an example. Thanks!

(Gosha Saveiko) #3

You can see in example. BUt shortly JSON arrays:

["sea", //what we search

  ["sears", //list of suggestions

   "search engines",

   "search engine",



   "seattle times"],

  ["7,390,000 results", //list of descriptions

   "17,900,000 results",

   "25,700,000 results",

   "1,220,000,000 results",

   "1 result",

   "17,600,000 results"],

  ["", //list of url






Positions in array are fixed. I don`t know how to say it in english:


Mozilla not supports format with urls, that`s why I ask for redirect if only 1 result found.

(Qiang Xue) #4

Is description required?

Regarding redirect, do you mean if there is only one result, instead of returning json results, we should just send a redirect header?

(Qiang Xue) #5

Can you make the plugin in English so that more users can use it? After it is in good shape, I can add a link to the plugin on our website.

(Gosha Saveiko) #6

It`s multilang:

I gave you you russian link - sorry.

About redirect - yes.

If I click on suggestion in IE - browser goes to defined in urls array page. But if i click on suggestion in FF - browser goes in search page with suggested query (FF not supports opensearch links) and finds here only 1 entry. After click on it - we go to correct page. That`s why straight redirect if 1 search result will be good for search form.

(Gosha Saveiko) #7

I simply use classname for description.

But you can provide blank array without descriptions


(Qiang Xue) #8

I just added support for this. (you need to put json=1 in the query part)

Please let me know if this works well. Thanks!

(Gosha Saveiko) #9

It works!. I`ve updated this plugin.

But there are some troubles:

  1. You forget to add "-detail" if query is about method or property.



  1. Because of big quantity of suggestions:

in FF it works in slow letter by letter input(((

in IE it works if there is not many suggestions (<=20 in tests)

(Qiang Xue) #10

I just enabled the limit GET parameter. You can set it to a smaller number to make it work fast.

Regarding "-detail", this is because a method could be defined in a base class whose detail description is only available in the base class API page.

(Gosha Saveiko) #11

OK. Now it works more stable.

you can save yiisearch.xml:

<?xml version="1.0" encoding="UTF-8"?>

<OpenSearchDescription xmlns=""



<Description>Yii Class Referrence Searcher</Description>



<Image width="16" height="16" type="image/x-icon"></Image>

<Url type="text/html" method="GET" xmlns:referrer="" template="{searchTerms}&amp;type=api-suggest"/>

<Url type="application/x-suggestions+json" template="{searchTerms}&amp;json=1&amp;limit=20" />



in the root directory of your site.

and add in head section:

<link rel="search" type="application/opensearchdescription+xml" title="Yiisearch"

        href="/yiisearch.xml" />

After that IE and FF will notify user, that there is opensearch plugin. By highlighting choose arrow in fastsearch panel. And make Google Chrome able to find opensearch and suggester on this site.

And will it possible about redirect if the only variant found? It makes Firefox to go directly to suggested API page.

(Qiang Xue) #12

Added the xml file and changed the head section. It is working nicely! Thank you very much!

Regarding the redirection, it is a bit complicated. I will need to ask Wei for help.

Is your FF search plug-in still needed?

Now I need to write something to advertise this. Thanks!

(Qiang Xue) #13

Could you please explain how to make it work for Chrome?

Also, it seems IE8 doesn’t give autocompletion. Is this expected?

(Qiang Xue) #14

Or even better, if you could write a wiki article explaining how to make use of this wonderful thing.

I will add a link to the article then.

(Gosha Saveiko) #15

This plugin duplicated xml-flie.

But firefox add-ons make plugin searching easier for users. And the can make own plugin collections and provide it to other users. Let it be in firefox too.

Right Click in the address bar and select Edit Search Engines and on the next window select Yiisearch and Edit.

here you can change alias. searching will be able as {alias}<TAB or SPACE>{query}. Autocomplete working nicely.

You make changes to search form URL - but it was wery handy to search only in API Docs. May be it would be good to make Yii Site Search, and Yii Api Search in 2 xml files. and 2 header links.



It`s very handy to experienced programmers. In russian jabber yii conference - they were dissapointed searching in all site.

I have IE8 - completion works more narrow then in FF.

Check if completion is on. Click on search engines chooser (arrow)

check something like Search plugins Management (I have russian version ant can`t say more exactly). And check if completion is on for Yii Search. if it is on. try to enter letters more slowly.

My eng is not great to do it))). But I can try.

P.S.> for Opera


Name: Yii Search

Keyword: yii


Then search by "yii<space>searchstr" but Opera not works with suggestions.

Now Ill going sleepin for 8h.)))

(Alexander Makarov) #16

Nice. I’ve got it working in Opera. It requires some config editing.

How to do it:

  1. Go to

  2. Put cursor on the left column search field and in context menu select "create search".

  3. Enter "yii" to keyword field and save.

  4. Close Opera.

  5. Go to Opera’s profile directory.

  6. Open search.ini.

  7. Find your search section by searching "yii". It should look like:

[Search Engine 24]



  1. Add the following to the end of the section:

Suggest Protocol=JSON

Suggest URL={SearchTerm}&json=1

  1. Save.

  2. Start Opera and try typing "yii ca" in your address bar.

(Gosha Saveiko) #17

samdark - Молодец…))) А я не думал, что саджест к опере прикрутить мона. Можно вдвоем статейку накидать)))

(Alexander Makarov) #18

It seems there is a problem with cache headers. Yii website should send nocache headers to prevent wrong suggestions.

(Alexander Makarov) #19


Good suggestion about two separate opensearches. Separate API search is a must have.

(Antonio) #20

Thank you very much guys for your work… Works nicely