使用CURL、googlecse在PHP中获取URL的若干内容

使用CURL、googlecse在PHP中获取URL的若干内容,php,json,curl,google-custom-search,Php,Json,Curl,Google Custom Search,我正在进行使用谷歌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'

我正在进行使用谷歌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;

?>

您忘记了很多事情,比如参数和连接。正确的功能是这样的

更新

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;
    }
}
函数getResult($key,$cseNumber,$keyword){//添加了参数

for($i=1;$i啊,是的。我已经添加了参数,但它仍然提供了一个空白页。:(它提供了完全空白的页,。我该怎么办?注释掉for循环并检查它是否按照您所说的那样提供了10个结果的输出是10个结果,使用$start='1'和echo$文件。但是如果我使用for循环($start=''并返回$file),我在for循环中写入echo,它不工作,只是一个空白页。函数getResult($url,$ref,$p,$start){for($I=0;$I)(抱歉,我不知道如何在注释中放入代码)
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;
    }
}