Monday 13 June 2016

Google Custom Search API

Google Custom Search API

Custom Search API lets you pro grammatically used to custom search engines for your site(s), you can search text OR images.


Format of URL
https://www.googleapis.com/customsearch/v1?q={SEARCH_TEXT}&cx={SEARCH_ENGINE_ID}&key={YOUR_API_KEY}



Question: What is SEARCH_TEXT?
It is search text which user want to search like "php", "interview questions", "web technology experts" etc.


Question: How to get SEARCH_ENGINE_ID?
  • Login to your gmail account
  • https://cse.google.com/cse
  • Create a new "Search Engine" or Us Existing one
  • Go to "Search Engine" from listing "Search Engine Listing"
  • Enable the "Image search"
  • Search "Sites to search" and select "Search on Entire Web"
  • Get the Search Engine ID, My search engine id is "0150557dddd364721200:zrndkpkfwyga"



Question: How to get YOUR_API_KEY?
If you don't have API_KEY, please get the API Key from http://code.google.com/apis/console#access .
You must need google account.


Question: How to extract data from custom search with text PHP?

$url='https://www.googleapis.com/customsearch/v1?q=php&cx={SEARCH_ENGINE_ID}&key={APP_KEY}';
try{
    echo $jsonData=file_get_contents($url);die;
    $searchData = json_decode($jsonData);
}  catch (Exception $e){
    echo $e->getMessage();
}
if(!empty($searchData->items)){
    foreach($searchData->items as $item){        
        echo $text = $item->title;        
        echo "\n";
    }    
}



Output

PHP: Hypertext Preprocessor
PHP - Wikipedia, the free encyclopedia
PHP 5 Tutorial
PHP | Codecademy
PHP Tutorial
PHP For Windows: Binaries and sources Releases
PHP: The Right Way
Physicians Health Plan of Northern Indiana, Inc. (PHPNI)
Newest 'php' Questions - Stack Overflow
PHP Programming - Wikibooks, open books for an open world



Question: What is URL of Google Explorer API ?
https://developers.google.com/apis-explorer/?hl=en_US#p/customsearch/v1/search.cse.list


Question: Give me example of Full data return by API in JSON Format?

{ "kind": "customsearch#search", "url": { "type": "application/json", "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json" }, "queries": { "request": [ { "title": "Google Custom Search - php", "totalResults": "508000000", "searchTerms": "php", "count": 10, "startIndex": 1, "inputEncoding": "utf8", "outputEncoding": "utf8", "safe": "off", "cx": "015055756665264721200:zrnkpkfwyga" } ], "nextPage": [ { "title": "Google Custom Search - php", "totalResults": "508000000", "searchTerms": "php", "count": 10, "startIndex": 11, "inputEncoding": "utf8", "outputEncoding": "utf8", "safe": "off", "cx": "015055756665264721200:zrnkpkfwyga" } ] }, "context": { "title": "My Test Website" }, "searchInformation": { "searchTime": 0.526994, "formattedSearchTime": "0.53", "totalResults": "508000000", "formattedTotalResults": "508,000,000" }, "items": [ { "kind": "customsearch#result", "title": "PHP: Hypertext Preprocessor", "htmlTitle": "\u003cb\u003ePHP: Hypertext Preprocessor\u003c/b\u003e", "link": "http://php.net/", "displayLink": "php.net", "snippet": "3 days ago ... Server-side HTML embedded scripting language. It provides web developers \nwith a full suite of tools for building dynamic websites: native APIs ...", "htmlSnippet": "3 days ago \u003cb\u003e...\u003c/b\u003e Server-side HTML embedded scripting language. It provides web developers \u003cbr\u003e\nwith a full suite of tools for building dynamic websites: native APIs ...", "cacheId": "s9KZuwBTtSkJ", "formattedUrl": "php.net/", "htmlFormattedUrl": "\u003cb\u003ephp\u003c/b\u003e.net/", "pagemap": { "cse_thumbnail": [ { "width": "81", "height": "77", "src": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQ0eKPRpNFSCbNpy4gCBZEwPVyUH9nZyQ_XbcbXanWV8ZarBAKse5lA-A" } ], "metatags": [ { "viewport": "width=device-width, initial-scale=1.0" } ], "cse_image": [ { "src": "http://php.net//images/news/php10thbd.jpg" } ] } }, { "kind": "customsearch#result", "title": "PHP - Wikipedia, the free encyclopedia", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e - Wikipedia, the free encyclopedia", "link": "https://en.wikipedia.org/wiki/PHP", "displayLink": "en.wikipedia.org", "snippet": "PHP is a server-side scripting language designed for web development but also \nused as a general-purpose programming language. Originally created by ...", "htmlSnippet": "\u003cb\u003ePHP\u003c/b\u003e is a server-side scripting language designed for web development but also \u003cbr\u003e\nused as a general-purpose programming language. Originally created by ...", "cacheId": "qU2AN8Gt0LkJ", "formattedUrl": "https://en.wikipedia.org/wiki/PHP", "htmlFormattedUrl": "https://en.wikipedia.org/wiki/\u003cb\u003ePHP\u003c/b\u003e", "pagemap": { "cse_thumbnail": [ { "width": "160", "height": "80", "src": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcREVCk76Rs7cXwmEsYOZPAzcDG4lixebNeXhy4ziXFrfgTtKQ4bCD13PQ" } ], "metatags": [ { "referrer": "origin-when-cross-origin" } ], "cse_image": [ { "src": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/PHP_Logo,_text_only.svg/200px-PHP_Logo,_text_only.svg.png" } ] } }, { "kind": "customsearch#result", "title": "PHP 5 Tutorial", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e 5 Tutorial", "link": "http://www.w3schools.com/php/", "displayLink": "www.w3schools.com", "snippet": "PHP is a server scripting language, and a powerful tool for making dynamic and \n... At W3Schools you will find complete references of all PHP functions:.", "htmlSnippet": "\u003cb\u003ePHP\u003c/b\u003e is a server scripting language, and a powerful tool for making dynamic and \u003cbr\u003e\n... At W3Schools you will find complete references of all \u003cb\u003ePHP\u003c/b\u003e functions:.", "cacheId": "8VKxSW_v5GEJ", "formattedUrl": "www.w3schools.com/php/", "htmlFormattedUrl": "www.w3schools.com/\u003cb\u003ephp\u003c/b\u003e/", "pagemap": { "metatags": [ { "viewport": "width=device-width, initial-scale=1" } ] } }, { "kind": "customsearch#result", "title": "PHP | Codecademy", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e | Codecademy", "link": "https://www.codecademy.com/learn/php", "displayLink": "www.codecademy.com", "snippet": "Learn to program in PHP, a widespread language that powers sites like \nFacebook.", "htmlSnippet": "Learn to program in \u003cb\u003ePHP\u003c/b\u003e, a widespread language that powers sites like \u003cbr\u003e\nFacebook.", "cacheId": "KACW1XKkN38J", "formattedUrl": "https://www.codecademy.com/learn/php", "htmlFormattedUrl": "https://www.codecademy.com/learn/\u003cb\u003ephp\u003c/b\u003e", "pagemap": { "metatags": [ { "viewport": "width=device-width, initial-scale=1.0", "csrf-param": "authenticity_token", "csrf-token": "yCGsG/vOZD+nnEe9mhqr3+gC4Ibrdi/K8dTzrbbbxpk=", "fb:app_id": "212500508799908", "og:url": "https://www.codecademy.com/learn/php", "og:site_name": "Codecademy", "og:type": "article", "og:title": "PHP", "og:description": "Learn to program in PHP, a widespread language that powers sites like Facebook." } ] } }, { "kind": "customsearch#result", "title": "PHP Tutorial", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e Tutorial", "link": "http://www.tutorialspoint.com/php/", "displayLink": "www.tutorialspoint.com", "snippet": "PHP Tutorial for Beginners - A simple and short PHP tutorial and complete \nreference manual for all built-in PHP functions. This tutorial is designed for \nbeginners ...", "htmlSnippet": "\u003cb\u003ePHP\u003c/b\u003e Tutorial for Beginners - A simple and short \u003cb\u003ePHP\u003c/b\u003e tutorial and complete \u003cbr\u003e\nreference manual for all built-in \u003cb\u003ePHP\u003c/b\u003e functions. This tutorial is designed for \u003cbr\u003e\nbeginners ...", "cacheId": "pZNex8X8UWIJ", "formattedUrl": "www.tutorialspoint.com/php/", "htmlFormattedUrl": "www.tutorialspoint.com/\u003cb\u003ephp\u003c/b\u003e/", "pagemap": { "cse_thumbnail": [ { "width": "204", "height": "155", "src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ8PER4KIXsaae0nNy3Na4XIoSGeqH83TGs4r5Eo1lSKGDVDH2_RCJi1M8" } ], "metatags": [ { "viewport": "width=device-width,initial-scale=1.0,user-scalable=yes", "og:locale": "en_US", "og:type": "website", "fb:app_id": "471319149685276", "og:site_name": "www.tutorialspoint.com", "apple-mobile-web-app-capable": "yes", "apple-mobile-web-app-status-bar-style": "black", "author": "tutorialspoint.com" } ], "cse_image": [ { "src": "http://www.tutorialspoint.com/php/images/php-mini-logo.jpg" } ] } }, { "kind": "customsearch#result", "title": "PHP For Windows: Binaries and sources Releases", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e For Windows: Binaries and sources Releases", "link": "http://windows.php.net/download/", "displayLink": "windows.php.net", "snippet": "This site is dedicated to supporting PHP on Microsoft Windows. It also supports \nports of ... PHP 7.1. 7.1 has no release. PHP 7.0 (7.0.7). Download source code ...", "htmlSnippet": "This site is dedicated to supporting \u003cb\u003ePHP\u003c/b\u003e on Microsoft Windows. It also supports \u003cbr\u003e\nports of ... \u003cb\u003ePHP\u003c/b\u003e 7.1. 7.1 has no release. \u003cb\u003ePHP\u003c/b\u003e 7.0 (7.0.7). Download source code ...", "cacheId": "LZ-At1_VxXEJ", "formattedUrl": "windows.php.net/download/", "htmlFormattedUrl": "windows.\u003cb\u003ephp\u003c/b\u003e.net/download/" }, { "kind": "customsearch#result", "title": "PHP: The Right Way", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e: The Right Way", "link": "http://www.phptherightway.com/", "displayLink": "www.phptherightway.com", "snippet": "4 days ago ... An easy-to-read, quick reference for PHP best practices, accepted coding \nstandards, and links to authoritative PHP tutorials around the Web.", "htmlSnippet": "4 days ago \u003cb\u003e...\u003c/b\u003e An easy-to-read, quick reference for \u003cb\u003ePHP\u003c/b\u003e best practices, accepted coding \u003cbr\u003e\nstandards, and links to authoritative \u003cb\u003ePHP\u003c/b\u003e tutorials around the Web.", "cacheId": "SkEDu_-C2_YJ", "formattedUrl": "www.phptherightway.com/", "htmlFormattedUrl": "www.\u003cb\u003ephp\u003c/b\u003etherightway.com/", "pagemap": { "metatags": [ { "og:image:url": "http://www.phptherightway.com/images/og-image.png", "og:image:width": "1024", "og:image:height": "640", "og:title": "PHP: The Right Way", "og:description": "An easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative PHP tutorials around the Web", "og:url": "http://www.phptherightway.com", "og:site_name": "PHP: The Right Way", "og:type": "website", "viewport": "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" } ] } }, { "kind": "customsearch#result", "title": "Physicians Health Plan of Northern Indiana, Inc. (PHPNI)", "htmlTitle": "\u003cb\u003ePhysicians Health Plan\u003c/b\u003e of Northern Indiana, Inc. (PHPNI)", "link": "http://www.phpni.com/", "displayLink": "www.phpni.com", "snippet": "We care about your healthcare needs. PHP understands the business of \nhealthcare and knows how important good health is to the quality of life. Our staff \nof ...", "htmlSnippet": "We care about your healthcare needs. \u003cb\u003ePHP\u003c/b\u003e understands the business of \u003cbr\u003e\nhealthcare and knows how important good health is to the quality of life. Our staff \u003cbr\u003e\nof ...", "cacheId": "kgzNo6SonKwJ", "formattedUrl": "www.phpni.com/", "htmlFormattedUrl": "www.\u003cb\u003ephp\u003c/b\u003eni.com/", "pagemap": { "cse_thumbnail": [ { "width": "182", "height": "143", "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQgzIyBIT9mt4ob6wj9DsJ4qzDMZFR-EGRL2ZPbGhYzntiGwsdiG6UYbJtt" } ], "cse_image": [ { "src": "http://www.phpni.com/Images/Misc/HIXUracSeal2019.png" } ] } }, { "kind": "customsearch#result", "title": "Newest 'php' Questions - Stack Overflow", "htmlTitle": "Newest '\u003cb\u003ephp\u003c/b\u003e' Questions - Stack Overflow", "link": "http://stackoverflow.com/questions/tagged/php", "displayLink": "stackoverflow.com", "snippet": "I'm having a bad brain fart... Ok, so i'm looking for a simple php script that will look \nthrough my database for a username and echo a column. The column I want ...", "htmlSnippet": "I'm having a bad brain fart... Ok, so i'm looking for a simple \u003cb\u003ephp\u003c/b\u003e script that will look \u003cbr\u003e\nthrough my database for a username and echo a column. The column I want ...", "cacheId": "4h3qSIyRzagJ", "formattedUrl": "stackoverflow.com/questions/tagged/php", "htmlFormattedUrl": "stackoverflow.com/questions/tagged/\u003cb\u003ephp\u003c/b\u003e", "pagemap": { "cse_thumbnail": [ { "width": "225", "height": "225", "src": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQrqiMqeE25uz0622Vj9Cvxf2QlczrckZNpgcCthUPsXMgvzR_AktvSQoMz" } ], "qapage": [ { "image": "http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a", "primaryimageofpage": "http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a", "title": "Newest 'php' Questions", "name": "Newest 'php' Questions", "description": "Q&A for professional and enthusiast programmers" } ], "metatags": [ { "twitter:card": "summary", "twitter:domain": "stackoverflow.com", "og:type": "website", "og:image": "http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a", "twitter:title": "Newest 'php' Questions", "twitter:description": "Q&A for professional and enthusiast programmers", "og:url": "http://stackoverflow.com/questions/tagged/php" } ], "cse_image": [ { "src": "http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a" } ] } }, { "kind": "customsearch#result", "title": "PHP Programming - Wikibooks, open books for an open world", "htmlTitle": "\u003cb\u003ePHP\u003c/b\u003e Programming - Wikibooks, open books for an open world", "link": "https://en.wikibooks.org/wiki/PHP_Programming", "displayLink": "en.wikibooks.org", "snippet": "A wikibook about programming in PHP. Also provides external links for more \ninformation.", "htmlSnippet": "A wikibook about programming in \u003cb\u003ePHP\u003c/b\u003e. Also provides external links for more \u003cbr\u003e\ninformation.", "cacheId": "eY2r0GBtEgkJ", "formattedUrl": "https://en.wikibooks.org/wiki/PHP_Programming", "htmlFormattedUrl": "https://en.wikibooks.org/wiki/\u003cb\u003ePHP\u003c/b\u003e_Programming", "pagemap": { "cse_thumbnail": [ { "width": "183", "height": "275", "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcS13jbwM9UIs5ekUya3TcycwhkH89RXdXRjtasahlTQzc-uwPOTPbNBZDTa" } ], "metatags": [ { "referrer": "origin-when-cross-origin" } ], "cse_image": [ { "src": "https://upload.wikimedia.org/wikipedia/commons/f/ff/PHPWikibookCover.png" } ] } } ] }