📜 ⬆️ ⬇️

Search API for PHP (Flash, Java and others)

Objective: to organize a search throughout the site with as little effort as possible and format the results in your own style.

Reasoning
There are various server systems for organizing full-text search, indexing a site, indexing a database. But they need to be installed on your server, set up, and some of them are also paid.
At some point I decided to use the Google Ajax Search API via JavaScript, but here it’s quite difficult to change the type of results, and you also need to make requests to another domain using javascript.

Decision
As a result, I came across the Code Snippets section on the Google AJAX Search API page and everything became simple: a little php code and full text search on any site is ready.
')

Php


A slightly simplified code snippet is shown. Of course, it would be good to make a check on the availability of the ajax.googleapis.com server at the moment.

<?php
//
$data = MyDB:: get () -> selectOne( '*' ,self::TABLE, '`id_mod` = ' .$ this ->id_mod);

// ?
$sireUrl = $data[ 'url' ];

//
$sigs = array(
'q' => array( 'type' => 'string' , 'required' => false ),
'start' => array( 'type' => 'integer' , 'required' => false )
);
$reqData = SpeData::sanitize_vars($ this ->queryArray, $sigs, 'RequestException' );
$q = urlencode($reqData[ 'q' ]. ' site:' .$sireUrl);
$start = empty($reqData[ 'start' ]) ? 0 : $reqData[ 'start' ];

// , :)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$q&rsz=large&hl=ru&start=$start" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://$sireUrl/" );
$body = curl_exec($ch);
curl_close($ch);

// , , :)
$json = json_decode($body);
// print_r($json)
// smarty ,

$search = array();

//
if (count($json -> responseData -> results) == 0) { //
$search[ 'result' ] = false ;
} else {
foreach ($json -> responseData -> results as $v) {
$search[ 'result' ][] = array(
'GsearchResultClass' => $v -> GsearchResultClass,
'unescapedUrl' => $v -> unescapedUrl,
'url' => $v -> url,
'visibleUrl' => $v -> visibleUrl,
'cacheUrl' => $v -> cacheUrl,
'title' => $v -> title, // ( html-)
'titleNoFormatting' => $v -> titleNoFormatting,
'content' => $v -> content //
);
}
}

//
if (count($json -> responseData -> results) == 0) { //
$search[ 'pages' ] = false ;
} else {
$url = 'http://' .$_SERVER[ 'HTTP_HOST' ].$_SERVER[ 'REDIRECT_URL' ]. '?q=' .$reqData[ 'q' ];
foreach ($json -> responseData -> cursor -> pages as $v) {
$search[ 'pages' ][] = array(
'start' => $v -> start,
'startUrl' => $url. '&start=' .$v -> start,
'label' => $v -> label
);
}
}

//
$currentPageIndex = $json -> responseData -> cursor -> currentPageIndex;
$search[ 'info' ] = array(
'q' => $reqData[ 'q' ],
'estimatedResultCount' => $json -> responseData -> cursor -> estimatedResultCount,
'moreResultsUrl' => $json -> responseData -> cursor -> moreResultsUrl,
'currentPageIndex' => $currentPageIndex,
'currentLabel' => $currentPageIndex + 1,
'startResult' => $currentPageIndex * 8 + 1,
'endResult' => ($currentPageIndex * 8 + 1) + count($search[ 'result' ]),
'next' => (count($search[ 'pages' ]) > $currentPageIndex + 1) ? $search[ 'pages' ][$currentPageIndex + 1][ 'startUrl' ] : false ,
'prev' => ($currentPageIndex) ? $search[ 'pages' ][$currentPageIndex - 1][ 'startUrl' ] : false
);


//
MySmarty:: get () -> assign( 'search' , $search);

?>


* This source code was highlighted with Source Code Highlighter .


SMARTY


I use the delimiters "{{" and "}}" to distinguish smarty from javascript

{{ if !$search.result}}
<b> {{$search.info.q}} </b> .
{{ else }}
<b>{{$search.info.startResult}} - {{$search.info.endResult}}</b> <b>{{$search.info.estimatedResultCount}}</b> <b>{{$search.info.q}}</b>
<br />
<br />

{{section name=i loop=$search.result}}
<a href= "{{$search.result[i].url}}" >{{$search.result[i].title}}</a><br />
{{$search.result[i].content}}
<br />
<br />

{{/section}}

<br/>

{{ if $search.info.prev}}
<a href= "{{$search.info.prev}}" ></a>
{{/ if }}


{{section name=i loop=$search.pages}}
{{ if $search.info.currentLabel == $search.pages[i].label}}
{{$search.pages[i].label}}
{{ else }}
<a href= "{{$search.pages[i].startUrl}}" >{{$search.pages[i].label}}</a>
{{/ if }}
{{/section}}

{{ if $search.info.next}}
<a href= "{{$search.info.next}}" ></a>
{{/ if }}

{{/ if }}


* This source code was highlighted with Source Code Highlighter .


So we got a good search for any kind of data (html, doc, pdf and other documents that google is looking for).

There is also a handy tool for Google Ajax Api Playground tests.

Habraludi, if you know, please tell me how things are with the agreements on the use of this search API. Should I indicate on the results page that I use google search, or do something else like that?

Thanks for attention.

PS This is my first post on Habré, if I did something wrong, say so, correct it :)

Source: https://habr.com/ru/post/54012/


All Articles