doutan1637 2013-05-17 04:11
浏览 42
已采纳

使用CURL,Google CSE在PHP中获取URL的多个内容

I'm working on my project of using Google CSE. At this step I'm writing codes to retrieve JSON results from it. Here's the code:

<?php
function cURL($url, $ref, $p) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if ($p) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
    return $result;
} else {
    return '';
}
}

if (isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
} else {
echo 'salah bro!';
}
$cseNumber = 'aaa'; // Key for the API thing
$key = 'bbb'; //Key for Nofriani's account: sorta a password
$start = '1';
$file = cURL('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, null);
echo $file;

?>

It worked perfectly and I got 10 first results of Google CSE. Unfortunately, the API restricts that only 10 results can be retrieved in bulk. Now, I intend to take 100 results in one results page (not in separated 10 page), by doing a looping. I added this:

$start= '';
$file = cURL('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, null, $start);
return $file;

function getResult() {
    for ($i = 0; $i <= 90; $i+=10) {
        $file .= cURL($url, $ref, $p, ($i + 1));
        for ($j = 0; $j < 10; $j++) {
            echo $file;
        }
    }
}

?>

But it didn't work just like it did. I tried some tips here: PHP How can I open several sources using curl? But it didn't work as well, :( Can anyone please help me solve this? Thanks..

  • 写回答

1条回答 默认 最新

  • doumowu7371 2013-05-17 04:16
    关注

    You forgot so many things like parameter and concatenation. Proper function will be like this

    Update

    function getResult($key,$cseNumber,$keyword) { // parameter added
        for ($i = 1; $i <= 10; $i++) {
            $file = cURL('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $i, 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $i, null); // . removed from here
            echo $file;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择