【问题标题】:Getting several contents of URL in PHP using CURL, Google CSE使用 CURL、Google CSE 在 PHP 中获取多个 URL 内容
【发布时间】:2013-05-17 04:11:22
【问题描述】:

我正在开发使用 Google CSE 的项目。在这一步,我正在编写代码以从中检索 JSON 结果。 代码如下:

<?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;

?>

效果很好,我得到了 10 个 Google CSE 的第一个结果。不幸的是,API 限制只能批量检索 10 个结果。现在,我打算通过循环将 100 个结果放在一个结果页面中(而不是在单独的 10 页中)。我添加了这个:

$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;
        }
    }
}

?>

但它并没有像以前那样工作。我在这里尝试了一些提示:PHP How can I open several sources using curl? 但它并没有那么好,:( 谁能帮我解决这个问题?谢谢..

【问题讨论】:

    标签: php json curl google-custom-search


    【解决方案1】:

    你忘记了很多东西,比如参数和连接。正确的功能会是这样的

    更新

    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;
        }
    }
    

    【讨论】:

    • 啊,是的。我已经添加了参数,但它仍然给出了一个空白页。 :(
    • 它给出了完全空白的页面,。我该怎么办?
    • 注释掉 for 循环并检查它是否按照您所说的输出 10 个结果
    • 是的,它有 10 个结果,$start='1' 和 echo $file。但是如果我使用for循环($start =''并返回$file),我在for循环中写了echo,它不起作用,只是一个空白页。
    • function getResult($url, $ref, $p, $start) { for ($i = 0; $i (对不起,我不知道如何将代码放在评论中)
    猜你喜欢
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2018-09-09
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多