phpにおけるcurl類の常用方法のパッケージと詳細(転載と整理)

4820 ワード

/* 
*   : http://www.xuehuwang.com/ 
*   :      
*    :          
*/
class CURL{    
  var $cookie_file;  //   Cookie             
  var $loginurl;//       
  var $actionstr;//      
  function __construct(){  
    $this->cookie_file=dirname(__FILE__)."/cookie_".md5(basename(__FILE__)).".txt";   
    if(!file_exists($this->cookie_file)){ //   Cookie          
      $str = $this->vget('jroam'); //              
      preg_match("/name="formhash" value=";(.*?)"/is",$str,$hash); //             
      $this->vlogin($this->loginurl,$this->actionstr); //     Cookie      
    }   
  }  
           
  function vlogin($url,$data){ //       Cookie        
    $curl = curl_init(); //     CURL        
    curl_setopt($curl, CURLOPT_URL, $url); //                         
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //                 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); //       SSL              
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //                 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //             
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //     Referer      
    curl_setopt($curl, CURLOPT_POST, 1); //        Post        
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post            
    curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie_file); //   Cookie             
    curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file); //         Cookie        
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); //                  
    curl_setopt($curl, CURLOPT_HEADER, 0); //      Header          
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //                     
    $tmpInfo = curl_exec($curl); //           
    if (curl_errno($curl)) {      
      echo 'Errno'.curl_error($curl);      
    }      
    curl_close($curl); //   CURL        
    return $tmpInfo; //           
  }      
             
  function vget($url){ //               
    $curl = curl_init(); //     CURL        
    curl_setopt($curl, CURLOPT_URL, $url); //                         
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //                 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); //       SSL              
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //                 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //             
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //     Referer      
    curl_setopt($curl, CURLOPT_HTTPGET, 1); //        Post        
    curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file); //         Cookie        
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); //                  
    curl_setopt($curl, CURLOPT_HEADER, 0); //      Header          
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //                     
    $tmpInfo = curl_exec($curl); //           
    if (curl_errno($curl)) {      
      echo 'Errno'.curl_error($curl);      
    }      
    curl_close($curl); //   CURL        
    return $tmpInfo; //           
  }      
             
  function vpost($url,$data){ //               
    $curl = curl_init(); //     CURL        
    curl_setopt($curl, CURLOPT_URL, $url); //                         
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //                 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); //       SSL              
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //                 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //             
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //     Referer      
    curl_setopt($curl, CURLOPT_POST, 1); //        Post        
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post            
    curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file); //         Cookie        
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); //                  
    curl_setopt($curl, CURLOPT_HEADER, 0); //      Header          
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //                     
    $tmpInfo = curl_exec($curl); //           
    if (curl_errno($curl)) {      
      echo 'Errno'.curl_error($curl);      
    }      
    curl_close($curl); //   CURL        
    return $tmpInfo; //           
  }      
  function delcookie($cookie_file){ //   Cookie        
    @unlink($cookie_file); //           
  }      
 }
//     
$mycurl=new CURL(); 
$mycurl->loginurl="http://192.168.0.2/login.php";//     
$mycurl->actionstr="user_name=111&uspassword=1234";//   ; 
  
$url1="http://192.168.0.2/userlist.php"; 
$htmlcontent= $mycurl->vget($url1); //         
echo $htmlcontent;