dtvhqlc57127 2017-04-04 08:39
浏览 52

CSE Google Custom Page api显示50个结果PHP?

I'm trying to get the first 50 results of the Google CSE API with the following PHP code.

The problem is that it combines the two pages sow the results are getting messed up like the first position is the one of the first page and the second position is the second of the second page and so on. Can someone tell me what I am doing wrong here?

What I actually want to do is get the first 50 results in a array, but the code below gives me mixed results.

$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);
  • 写回答

1条回答 默认 最新

  • doushi6947 2017-04-04 16:43
    关注

    It looks like you are adding each set of 10 results into a separate array, so $items1 has the first 10 results, $items2 has the next 10, etc. If you want all 50 results in a single array, there is no need to use an index in the array's name.

    Also, the "start" parameter is the number of the result you want, not the number of the result set - so you want the first query to start at 1, the second to start at 11, the third at 21, etc.

    You may also want to check that there is something in the result before adding it to your array.

    I might do something more like so:

    $apiKey = "theapikey";
    $query = "news";
    $items = array();
    
    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 - 1)*10 + 1 );
    
      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 ) {
    
        if ($url = $result->link && trim($url)) $items[] = $url;
    
      } 
    
    }
    
    echo json_encode($items);
    

    Finally, a couple caveats:

    • There is an existing question about whether this JSON api is deprecated and possibly going away.
    • Each query for the next 10 results counts against your quota. If you are worried about running out of queries each month, or if you are paying for quota increases, you may want to consider only retrieving what you need.
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器