2011-10-09 67 views
0
<?php 

function cuload($url, $got = array()){ 

//include(MDL.'socket_adapter.php'); 
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"; 

$default_options = array(
    'data'   => 'og', 
    'post_data'  => false, 
    'referer'  => false, 
    'cookie'   => false, 
    'auth'    => false, 
    'proxy'   => false, 
    'pauth'   => false, 
    'returndata'  => true, 
); 

foreach($default_options as $opt=>$value) { 
    if(!isset($default_options[$opt])) {$got[$opt] = $value; } 
} 


$curl = curl_init(); 
//if(strstr($referer,"://")){ 
//curl_setopt ($curl, CURLOPT_REFERER, $got['referer']);} 
curl_setopt ($curl, CURLOPT_URL, $url); 
curl_setopt ($curl, CURLOPT_USERAGENT, $user_agent); 
curl_setopt ($curl, CURLOPT_HEADER, 1); 
if(isset($got['returndata'])){ 
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); 
}else{ 
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false); 
} 
curl_setopt ($curl, CURLOPT_ENCODING, 'gzip, deflate'); 
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0); 

    //curl_setopt($curl ,CURLOPT_USERAGENT, $got_opt['user_agent']); //The Name of the UserAgent we will be using ;) 
    if(isset($got['post'])){curl_setopt($curl ,CURLOPT_POST , true); curl_setpot($curl ,CURLOPT_POSTFIELDS , $post); } 
    if(isset($got['referer'])) curl_setopt($curl,CURLOPT_REFERER, $options['referer']); 
    if(isset($got['cookie'])){ if($got['cookie'] == "0"){ 
    curl_setopt($curl ,CURLOPT_COOKIEJAR, TBP."cookie.txt"); }//If ever needed... 
    elseif($got['cookie'] != "0"){ 
    curl_setopt($curl ,CURLOPT_COOKIE, $got['cookie']); }} 
    //curl_setopt($curl ,CURLOPT_FOLLOWLOCATION, true); 
    //curl_setopt($curl ,CURLOPT_MAXREDIRS, 5); 
    //curl_setopt($curl ,CURLOPT_SSL_VERIFYPEER, false); 

    $custom_headers = array(); 
    $custom_headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
    $custom_headers[] = "Pragma: no-cache"; 
    $custom_headers[] = "Cache-Control: no-cache"; 
    $custom_headers[] = "Accept-Language: en-us;q=0.7,en;q=0.3"; 
    $custom_headers[] = "Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7"; 
    if(isset($url_parts['user']) and isset($url_parts['pass'])) { 
     $custom_headers[] = "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']); 
    }elseif(isset($got['auth'])){ 
    $uj = explode(":",$got['auth']); $custom_headers[] = "Authorization: Basic ".base64_encode($uj[0].':'.$uj[1]); } 
    if(isset($got['pauth'])){ 
     curl_setopt($curl ,CURLOPT_PROXYUSERPWD ,$pauth); } 
    if(isset($got['proxy'])){ 
     curl_setopt($curl ,CURLOPT_PROXY ,$proxy); } 

    curl_setopt($curl ,CURLOPT_HTTPHEADER, $custom_headers); 

    $response = curl_exec($curl); 
    $info = curl_getinfo($curl); 
curl_close ($curl); 
ob_end_clean(); 

return $response; 
} 

?> 

確定,當我試圖在同一頁上該腳本:捲髮不是現在正常工作

<?php 
$page = cuload('http://www.google.com'); 
?> 

但現在沒有呼應$ page變量它呼應的內容。我不需要在這裏回顯任何東西,只需要從$頁面變量內容中找到一些鏈接。深入探索腳本後,我發現可能是curl_exec中存在問題,因爲如果我刪除return $response仍然顯示內容!我不明白爲什麼它給出輸出$page當我不呼應$page。 請幫幫我!

+0

什麼是'ob_end_clean()'在你的函數做什麼? –

+0

@BradChristie它被錯誤粘貼在那裏!對不起。 –

回答

0

而不是

foreach($default_options as $opt=>$value) { 
if(!isset($default_options[$opt])) {$got[$opt] = $value; } 
} 

試試這個:

foreach($default_options as $opt=>$value) { 
if(!empty($default_options[$opt])) {$got[$opt] = $value;} 
} 
+0

@kukiepei感謝它的工作!順便說一句,爲什麼我面臨的問題,你會請時解釋它!謝謝 –

+0

'isset($ var)'的值即使在$ var設置爲false時也是如此,所以$ got ['returndata']爲空,CURLOPT_RETURNTRANSFER設置爲false不爲true。我希望你明白,我的英文不是很好 – kukipei

+0

謝謝!我明白你的英語很好!我希望我可以跟你談談關於PHP的一些問題。我的谷歌談話ID:ashso[email protected] –