2013-05-17 2 views
0

Google CSE 사용 프로젝트를 진행 중입니다. 이 단계에서 JSON 결과를 검색하는 코드를 작성합니다. 다음은 코드입니다 :CURL을 사용하여 PHP에서 URL의 여러 내용 가져 오기, Google CSE

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

?> 

그것은 완벽하게 작동하고 나는 구글 CSE의 10 개 첫 번째 결과를 얻었다. 불행하게도 API는 단지 10 개의 결과 만 대량으로 검색 할 수 있도록 제한합니다. 이제 루프를 수행하여 하나의 결과 페이지 (분리 된 10 페이지가 아닌)에서 100 개의 결과를 얻으 려합니다. 나는이 추가 :

$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?이 하지만, :( 이 사람이 나에게이 문제를 해결하는 데 도움 주실 래요 감사합니다 .. 당신은 매개 변수와 연결 같은 많은 일들을 잊어

답변

0

잘 작동하지 않았다 적절한 기능처럼 될 것입니다. 이

업데이트

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

아, 그래. 나는 매개 변수를 추가했습니다,하지만 여전히 빈 페이지를했다. :( – Fii

+0

그것은 1 일의에게? –

+0

모든 루프 빈 페이지를 주거나 보여주고있다 그것은 완전히 빈 페이지를 제공합니다. 이자형,. 나는 무엇을해야합니까? – Fii

관련 문제