我正在尝试从PHP进行程序化Google搜索,但无济于事。我得到了我所追求的JSON对象,但它有[totalResults] => 0,对于我搜索过的所有内容。我使用的apiKey是服务器密钥,customSearchEngineKey用于我通过说明here创建的自定义搜索引擎。我知道这个有用,因为我已经尝试过其他语言,但是不能使用otherLanguage作为最终的程序(我宁愿不说为什么),但是当我把它带到PHP时,由于某种原因,它失败了。为什么?
这是我的PHP代码,顺便说一下:
<?php
// turn on the use of session variables, if it has not already been done
session_start();
// declare function that creates the URL for the Google search associated with a query
function makeSearchString($query, $startNumber = 1, $count = 10)
{
// declare $apiKey,$customSearchEngineKey
$apiKey = "AIzaSyCW6jCkVPGWRd2PN1ZHeOKq8haJqkYqEwQ";
$customSearchEngineKey = "003207291839125798740:4fbhl2kr0mi";
// set up searchString with $apiKey,$customSearchEngineKey
$searchString = "https://www.googleapis.com/customsearch/v1?key=" . $apiKey . "&cx=" . $customSearchEngineKey . "&q=";
// split query into an array with ' ' as the delimiter (regex is "/[ ]+/")
$theQueries = explode("/[ ]+/", $searchString);
// for each subquery in theQueries
foreach ($theQueries as $subquery)
// append subquery to searchString
$searchString .= $subquery;
// specify that the response should be in JSON, in searchString
$searchString .= "&alt=json";
// try to turn safeSearch on
$searchString .= "&safe=high";
// if startNumber is not 1
if ($startNumber != 1)
// specify startNumber, in searchString
$searchString .= ("&start=" . $startNumber);
// if count is not 10
if ($count != 10)
// specify count, in searchString
$searchString .= ("&num=" . $count);
// return searchString
return $searchString;
}
/* Don't forget that you need to test this function. Try it with at least 2 searches... */
// declare function that returns the JSON associated with a Google search tied to query
function getJSONStringFor($query)
{
// if $query is a URL that contains the first few characters in the defaultSearchString
if (strpos($query, "https://www.googleapis.com/customsearch/v1?key=") !== false)
{
// return the file contents for that $query
return file_get_contents($query);
}
// makeSearchString for $query and get the JSONString from that searchString
return file_get_contents(makeSearchString($query));
}
// if the searchData in $_SESSION is not already initialized
if (!isset($_SESSION['searchData']))
// initialize it
$_SESSION['searchData'] = array();
// get the terms to search for /* where from, I don't know!! They should at least be an array of Strings */
/* For now, we can simply create an array of three terms to search for */
$terms = array("alopecia", "Superman", "Spiderman");
// for each term
foreach ($terms as $term)
{
// get the JSONString
// parse the JSONString into an associative array
$array = json_decode(getJSONStringFor($term), true);
print_r($array); /* returns no results for some reason */
// for each searchResult in that array
// if there is pagemap and it has a cse_image
// add it as a searchResult for that queryResult
// append queryResult to searchData
}
?>
答案 0 :(得分:0)
我明白了。问题出在我的makeSearchString()上,即这一行:
$theQueries = explode("/[ ]+/", $searchString);
当我试图将$searchString
拆分成数组时,我不小心告诉PHP编译器将$query
拆分成数组。所以,我把它改为$theQueries = explode("/[ ]+/", $query);
,一切顺利!