google custom search

2019-07-24 11:04发布

问题:

cant quite find the answer I need so hopefully you guys can help.

I am trying to develop ( with basic but growing knowledge!) a split search bar for a custom cake website.

basically I have one search bar in the header, and a drop down with whole site and cakes in it.

When the whole site is selected I want it to pass the search parameters to googel custom search and display on the page. when cakes is selected it triggers a custom php search I wrote to search for and display a single cake picture.

I am doing this at the moment with an if statement. so if type=cakes then ( run php () else put search terms through google custom search.

Now I have tried using the results only format for it, but it doesnt seem to render anything! I dont know how to pass my parameter to the results page. I tried setting the parameter to cakeref which is what i have specified previously.

Any help or explanations as to how google custom search works and integrates would be amazing.

回答1:

Try this

$search_query = "Is+Wikipedia+IsGreat";
$amazon_url = "https://www.googleapis.com/customsearch/v1?key=3425lh43lkh52lk34jh5&cx=023470239740:kj2h3kl4hlk23&alt=atom&num=5&q='$search_query'";
$google_search = file_get_contents($amazon_url);

You need API Key and CX number for calling this API. And urlencoding to encode query parameters.

Try Curl also..

API Client Library is here

http://code.google.com/p/google-api-php-client/

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/customSearch/index.php

http://www.phpclasses.org/package/6301-PHP-Perform-searches-with-the-Google-Site-Search-API.html