首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP Google搜索不返回任何内容

PHP Google搜索不返回任何内容
EN

Stack Overflow用户
提问于 2014-10-29 06:12:39
回答 1查看 231关注 0票数 0

我试图从PHP进行一个编程的Google搜索,但没有效果。我得到了我想要的JSON对象,但是对于我所搜索的所有东西,它都有totalResults => 0。我使用的apiKey是一个服务器密钥,而customSearchEngineKey是我通过指令这里创建的自定义搜索引擎。我知道这是可行的,因为我在otherLanguage中尝试过,但不能在最终程序中使用otherLanguage (我不想说原因),但是当我把它带到PHP中时,由于某种原因,它失败了。为什么??

顺便说一句,这是我的PHP代码:

代码语言:javascript
运行
复制
<?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
    }
?>
EN

Stack Overflow用户

回答已采纳

发布于 2014-10-30 00:22:55

我想通了。问题在于我的makeSearchString(),即这一行:

$theQueries = explode("/[ ]+/", $searchString);

当我试图将$searchString拆分成一个数组时,我无意中告诉PHP编译器将$query拆分成一个数组。所以,我把它改成了$theQueries = explode("/[ ]+/", $query);,一切都进行得很顺利!

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26623790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档