浏览代码

google cse pagination fix, thanks nikolay

lolcat 1 周之前
父节点
当前提交
5fc80a3672
共有 1 个文件被更改,包括 14 次插入5 次删除
  1. 14 5
      scraper/google_cse.php

+ 14 - 5
scraper/google_cse.php

@@ -910,11 +910,10 @@ class google_cse{
 			"image" => []
 		];
 		
-		// detect next page
-		if(
-			isset($json["cursor"]["isExactTotalResults"]) || // detects last page
-			!isset($json["cursor"]["pages"]) // detects no results on page
-		){
+		// A response can contain a final page of image results while also marking
+		// the cursor as exact/finished.  Parse those results before deciding
+		// whether another page token should be offered.
+		if(!isset($json["results"]) || !is_array($json["results"])){
 			
 			return $out;
 		}
@@ -939,6 +938,16 @@ class google_cse{
 			];
 		}
 		
+		// Only decide whether a following page exists after preserving every
+		// result returned on this page.
+		if(
+			isset($json["cursor"]["isExactTotalResults"]) || // detects last page
+			!isset($json["cursor"]["pages"]) // detects no results on page
+		){
+			
+			return $out;
+		}
+		
 		// get next page
 		$out["npt"] =			
 			$this->backend->store(