小编Ida*_*dan的帖子

Google Custom Search API start = 100会导致错误400

我有一个使用Google Custom Search API的脚本,遍历多个结果页面.

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=0&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=10&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=20&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com
Run Code Online (Sandbox Code Playgroud)

在上面的所有例子中,我得到了适当的回应.查询响应声称搜索结果有17,900个.但是当脚本达到start = 100时:

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=100&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com
Run Code Online (Sandbox Code Playgroud)

我收到以下响应(这是转换为PHP对象的JSON响应):

stdClass Object (
        [error] => stdClass Object
            (
                [errors] => Array
                    (
                        [0] => stdClass Object
                            (
                                [domain] => global
                                [reason] => invalid
                                [message] => Invalid Value
                            )
                    )
                [code] => 400
                [message] => Invalid Value
            ) )
Run Code Online (Sandbox Code Playgroud)

尽管我在start = 90中收到的结果声称下一页存在:

"nextPage": [
   {
    "title": "Google Custom Search - \"bank\"",
    "totalResults": "17900",
    "searchTerms": "\"bank\"",
    "count": 10,
    "startIndex": 100,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "[[CX VALUE]]",
    "sort": …
Run Code Online (Sandbox Code Playgroud)

php google-custom-search

11
推荐指数
1
解决办法
1514
查看次数

Mongo:$or 查询在同一字段中查找多个可能的值

我在 mongo 上有一个名为“reports”的集合,它可以让客户发表评论并进行讨论。

以下是每个报告中“讨论”数组的样子:

"discussions" : [
    {
        "user_id" : "david",
        "timestamp" : ISODate("2016-03-17T01:15:00Z"),
        "comment" : "I was wondering, did anyone else find it interesting?",
        "replies" : [
            {
                "user_id" : "kevin",
                "timestamp" : ISODate("2016-03-17T01:15:00Z"),
                "comment" : "Not really"
            }
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

由于每个评论上方显示的用户信息经常发生变化,我想使用引用并根据“user_id”字段从“users”集合中提取他们的信息,而不是将他们的信息嵌入到每个讨论帖中。我想灵活地分片整个数据库,所以“$lookup”可能是不行的(因为,如果我理解正确,“$lookup”中的“from”字段不能被分片)。

因此,我想构建一个类似于以下 SQL 的“$or”查询:

SELECT * FROM USERS WHERE id IN ('david', 'kevin')
Run Code Online (Sandbox Code Playgroud)

这将从用户集合中提取额外的用户信息,然后将它们与应用程序级别的其余评论详细信息结合起来。

出于这个原因,我推送到 find() 的查询是由应用程序构建的,通过迭代“报告”的结果并提取“user_id”。这是查询的 var_dump 的样子:

array(1) {
  ["$or"]=>
  array(1) {
    [0]=>
    array(1) {
      ["_id"]=>
      string(4) "david"
    },
    [1]=>
    array(1) {
        ["_id"]=>
        string(4) "kevin" …
Run Code Online (Sandbox Code Playgroud)

php mongodb

3
推荐指数
1
解决办法
4178
查看次数

PHP CURL将cookie保存到cookiejar中,但不使用它

我有一个PHP脚本,它使用CURL登录到带有简单登录页面的站点。它将初始请求发送到站点,并查看其是否已经登录(由于cookie)或登录页面是否出现-如果已登录,则登录。

但是,最近我注意到,每次脚本运行时,它都永远不会登录。使用VERBOSE深入探查头表明,从未使用过COOKIEFILE / COOKIEJAR中的cookie,只有站点为该特定会话接收的cookie 。如果我在运行过程中将cookie手动添加到cookiejar中(以前可以正常工作)-它不再起作用,因为实际上并未使用COOKIEFILE中的cookie。

这在本地和生产服务器上都发生,这意味着这似乎不是系统问题。我为其他登录页面创建了具有相同结果的测试版本。我使用cookie文件的完整路径(使用cookie更新,但未使用),并使用curl_close()。

以下是CURL函数:

private function curlPage($url, $postParameters) {
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postParameters);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__.'/cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__.'/cookie.txt');
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POSTREDIR, 3);
    if ($this->verbose == 1) curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->defaultTimeout); 
    curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
    $pageResponse = curl_exec($ch);
    curl_close($ch); 
    return $pageResponse;
}
Run Code Online (Sandbox Code Playgroud)

以下是CURL请求对主页的详细响应,应该检查该页面是否已登录。由于该站点是客户端站点,因此我对其进行了编辑。

* Rebuilt URL to: *********
* Hostname was NOT found in …
Run Code Online (Sandbox Code Playgroud)

php cookies curl

2
推荐指数
1
解决办法
8539
查看次数

MongoDB PHP 驱动程序:日期搜索不起作用

我正在从旧的 MongoDB 驱动程序迁移到 PHPLIB。不幸的是,我在尝试转换日期搜索时遇到了问题。我试图检索从某个日期(昨天)添加到数据库中的文档,但是,当我执行搜索时,无论“ added_on”中的值如何,我都会取回所有记录的计数。不知道我做错了什么。

    $collection = $mongo->getCollection("records");
$yesterday = new DateTime(date('Y-m-d').' 00:00:00');
$dateFrom = new MongoDB\BSON\UTCDateTime($yesterday->format('U'));
    $response = $collection->count(['added_on' => ['$gte' => $dateFrom], 'instance' => $element, 'invisible' => false]);
Run Code Online (Sandbox Code Playgroud)

UTCDateTime 的 var_dump 显示有效值:

对象(MongoDB\BSON\UTCDateTime)#477 (1) { ["毫秒"]=>
字符串(10) "1515954053" }

预期返回的文档示例:

{ "_id" : ObjectId("5a5badcffe23a278e2bb739a"), "instance": ObjectId("591555806803fa06650b474c"), " added_on" : ISODate("2018-01-14T25Z", "false"), "false"): “ : 真的 }

任何帮助,将不胜感激。

更新:删除发送到 UTCDateTime 对象的变量,如下所示:

$dateFrom = new MongoDB\BSON\UTCDateTime();
Run Code Online (Sandbox Code Playgroud)

有效(如上所示,我将来添加了一条记录)。问题是当我发送变量来指定我需要的时间戳时。

php mongodb

2
推荐指数
1
解决办法
1104
查看次数

标签 统计

php ×4

mongodb ×2

cookies ×1

curl ×1

google-custom-search ×1