我正在查询的谷歌API有100/day的低下限,我得到:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET ...

..并且脚本在那之后无能为力。

我如何防止它失败并至少保存到那时为止接收到的所有数据?我将数据保存在数组中:
function searchImages($service, $optParams, $query) {
  $results = $service->cse->listCse($query, $optParams);
  return $results;
}
$descriptionSearch = searchImages($customsearchService, $customsearchService_optParams, $descriptions[$i]);
foreach ($descriptionSearch->items as $item) {
  array_push($list[$item_codes[$i]], strtok($item->link,'?'));
}

最佳答案

function searchImages($service, $optParams, $query) {
  try {
      $results = $service->cse->listCse($query, $optParams);
  }catch (Exception $e) {
      // should log this exception... you can use Log4PHP
      return NULL;
  }
  return $results;
}

$descriptionSearch = searchImages($customsearchService,customsearchService_optParams, $descriptions[$i]);
if (!is_null($descriptionSearch)) {
    foreach ($descriptionSearch->items as $item) {
        array_push($list[$item_codes[$i]], strtok($item->link,'?'));
    }
}

10-06 05:34