2016-02-12 61 views
0

我寫在本地主機的PHP 這個代碼的工作,但在主機不工作在指數如何在谷歌與捲曲的PHP搜索

function cURL($url, $ref, $header, $cookie, $p){ 
    $ch = curl_init();//start curl 
    curl_setopt($ch, CURLOPT_URL, $url);   //curl Targeted URL 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
    curl_setopt($ch, CURLOPT_REFERER, $ref);  //fake referer 
    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 ''; 
    } 
} 

使用:

$file = cURL('https://www.google.com/search?num=30&hl=en&q=phpscript','http://www.google.ru',0,0,null); 
+1

什麼不行?你得到什麼錯誤? – cmorrissey

+0

谷歌積極監控並防止他們的內容被刮掉......世界上最大的數據刮板產生一些諷刺意味,但即使你得到這個工作,它也只會在極其有限的時間內這樣做。從嘗試過的人那裏拿出來。 –

+0

使用代理擺脫此方式 –

回答

0

您的主機可能會阻止機器人(?)簡化了一點 - 但這對我有用:

<?php 
/** 
* Created by CompuSolver.com. 
* User: Hank Castello 
* Date: 6/20/16 
* Time: 10:27 AM 
*/ 
function curlGet($url, $header){ 
    $ch = curl_init();//start curl 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_URL, $url);   //curl Targeted URL 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.8 [en] (Windows NT 5.1; U)'); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 

    $result = curl_exec($ch); 
    $httpResponse = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 
    return $result; 
} 
$targetURL = 'https://www.google.com/search?num=100&newwindow=1&site=&source=hp&q=web+developers&oq=web+developers&gs_l=hp.3..0l10.1547.5649.0.6045.15.11.0.4.4.0.115.760.8j1.9.0....0...1c.1.64.hp..2.12.714.0..0i131j0i3.xUKoFB0hpKM'; 
$header = array('Accept-Language: en-us,en;q=0.7,bn-bn;q=0.3','Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'); 
$responsePg = curlGet($targetURL, $header); 
echo $responsePg;