0
我试图用以下PHP代码获得Google CSE API的前50个结果。CSE Google自定义页面api显示50个结果PHP?
问题是,它结合了两个页面播种的结果越来越乱了,就像第一个位置是第一个页面,第二个位置是第二个页面,等等。有人能告诉我我在这里做错了吗?
我实际上想要做的是获得数组中的前50个结果,但下面的代码给了我不同的结果。
$apiKey = "theapikey";
$query = "news";
for ($i = 1; $i <= 5; $i++) {
$ch = curl_init();
$request = "https://www.googleapis.com/customsearch/v1?q=" . urlencode("$query") . "&cx=013594553343653397533:q-qkkaltmay" ."&key=" . $apiKey . "&start=" . $i;
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
$output = json_decode($output);
foreach($output->items as $result) {
$url = $result->link;
${"items" . $i}[] = $url;
}
}
echo json_encode($items1);
感谢您对已弃用v1的回答和澄清。当使用这个方法时,它返回一个空数组。 – Jan
另外,当记录$ request时,它会给出正确的url,但数组保持为空。 – Jan
首先:JSON/REST api上有更新:http://stackoverflow.com/questions/43041894/google-cse-rest-api-is-v1-deprecated-is-there-a-v2 –