2010-04-05 67 views
-1

我有一個叫做Curl的捲髮類。保存圖像的php-curl腳本---實際上是驗證碼圖像

讓我們假設我有這樣的代碼:

$url = 'http://www.google.com' 
$fields = array('q'=>'search term'); //maybe some other arguments. but let's keep it simple. 
$curl = new Curl(); 
$page = $curl->post($url,$fields); 

$頁面都會有一些圖片至極捲曲默認不加載它們。我需要知道如何在不使用捲曲的情況下保存特定圖像。一旦我使用$ page = $ curl-> post(..)我需要知道如何在不使用另一個$ curl-> post(_image_location_)來獲取該文件的情況下保存該圖像。

需要此功能的原因是從表單中保存驗證碼圖像。我需要訪問表單並獲取正在加載的特定圖像。如果我嘗試訪問圖像的URL,它將是一個不同的驗證碼圖像。

回答

1

你所描述的是不可能的。對於網頁中的每個外部資源(即任何不屬於HTML內容本身的內容,例如圖像,腳本,樣式表等),您都必須提出一個單獨的請求來檢索它。這就是所有瀏覽器的操作方式。

許多驗證碼在會話的基礎上工作。您向HTML頁面的初始請求可能會創建一個會話cookie,該cookie將作爲響應頭文件的一部分發回。當請求圖片時,這個cookie將會被預期。如果你只是對圖像做一個簡單的捲曲請求,你不會發送該cookie,因此你會得到一個不同的圖像。

您將不得不分析該頁面並確定究竟會進行什麼類型的會話管理,並適當修改您的Curl請求,但正如我所提到的,我懷疑它會基於Cookie。您可能需要查看CURLOPT_COOKIEJAR curl_setopt()參數才能開始。你也可以谷歌pretty straightforward examples以及。

1

這是entiree類,如果你有問題,我可以更好地解釋你。

<?php 

/** 
* Description of class-curl 
* 
* @author NEO 
*/ 
class cURL { 

    public $headers; 
    public $user_agent; 
    public $compression; 
    public $cookie_file; 
    public $proxy; 
    public $process; 
    public $url; 
    public $hash; 
    public $content; 

    public function __construct($url) { 
     $this->url = $url; 
     $this->process = curl_init($this->url); 
     $cookiename = uniqid('cookie_') . '.txt'; 
     $this->cURL($cookies = TRUE, $cookiename); 
    } 

    public function cURL($cookies = TRUE, $cookie = 'cookie.txt', $compression = 'gzip', $proxy = '') { 
     $this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; 
     $this->headers[] = 'Connection: Keep-Alive'; 
     $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
     $this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 
     $this->compression = $compression; 
     $this->proxy = $proxy; 
     $this->cookies = $cookies; 
     if ($this->cookies == TRUE) 
      $this->cookie($cookie); 
    } 

    public function cookie($cookie_file) { 
     if (file_exists($cookie_file)) { 
      $this->cookie_file = $cookie_file; 
     } else { 
      fopen($cookie_file, 'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions'); 
      $this->cookie_file = $cookie_file; 
      @fclose($this->cookie_file); 
     } 
    } 

    //Capturar todo el codigo fuente de la web solicitada 
    public function get() { 
     curl_setopt($this->process, CURLOPT_HTTPHEADER, $this->headers); 
     curl_setopt($this->process, CURLOPT_HEADER, 0); 
     curl_setopt($this->process, CURLOPT_USERAGENT, $this->user_agent); 
     if ($this->cookies == TRUE) { 
      curl_setopt($this->process, CURLOPT_COOKIEFILE, $this->cookie_file); 
      curl_setopt($this->process, CURLOPT_COOKIEJAR, $this->cookie_file); 
     } 
     curl_setopt($this->process, CURLOPT_ENCODING, $this->compression); 
     curl_setopt($this->process, CURLOPT_TIMEOUT, 90); 
     if ($this->proxy) 
      curl_setopt($this->process, CURLOPT_PROXY, $this->proxy); 
     curl_setopt($this->process, CURLOPT_RETURNTRANSFER, 1); 
     //curl_setopt($this->process, CURLOPT_FOLLOWLOCATION, 1); 
     $return = curl_exec($this->process); 
     //curl_close($this->process); 
     return $return; 
    } 

    public function post($data) { 
     curl_setopt($this->process, CURLOPT_HTTPHEADER, $this->headers); 
     curl_setopt($this->process, CURLOPT_HEADER, 1); 
     curl_setopt($this->process, CURLOPT_USERAGENT, $this->user_agent); 
     if ($this->cookies == TRUE) 
      curl_setopt($this->process, CURLOPT_COOKIEFILE, $this->cookie_file); 
     if ($this->cookies == TRUE) 
      curl_setopt($this->process, CURLOPT_COOKIEJAR, $this->cookie_file); 
     curl_setopt($this->process, CURLOPT_ENCODING, $this->compression); 
     curl_setopt($this->process, CURLOPT_TIMEOUT, 30); 
     if ($this->proxy) 
      curl_setopt($this->process, CURLOPT_PROXY, $this->proxy); 
     curl_setopt($this->process, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($this->process, CURLOPT_RETURNTRANSFER, 1); 
     //curl_setopt($this->process, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($this->process, CURLOPT_POST, 1); 
     $return = curl_exec($this->process); 
     //curl_close($this->process); 
     return $return; 
    } 

    public function error($error) { 
     echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>"; 
     die; 
    } 

    public function grab_image() { 
     //obener una imagen desde la url especificada, se puede mejorar para que 
     //se le de una url y me mande todas las imagenes que encuentre 
     curl_setopt($this->process, CURLOPT_HEADER, 0); 
     curl_setopt($this->process, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($this->process, CURLOPT_BINARYTRANSFER, 1); 
     $raw = curl_exec($this->process); 
     $name = explode("/", $this->url); 
     $name = array_pop($name); 
     if (file_exists($name)) { 
      unlink($name); 
     } 
     $fp = fopen($name, 'x'); 
     fwrite($fp, $raw); 
     fclose($fp); 
     return $name; 
     //return $raw; 
    } 

    public function cURLclose() { 
     unlink($this->cookie_file); 
     curl_close($this->process); 
     unset($this); 
    } 

    public function saveCaptcha($source) { 
     preg_match('/ipt" src="(h[^"]+)/', $source, $result); 
     $captcha = $this->get($result[1]); 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $result[1]); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $captcha = curl_exec($ch); 
      curl_close($ch); 
     $hash = explode("challenge :", $captcha); 
     $hash1 = explode("'", $hash[1]); 
     $cont = $hash1[1]; 
     $img = 'http://www.google.com/recaptcha/api/image?c=' . $cont; 
      $ch = curl_init($img); 
      curl_setopt($ch, CURLOPT_HEADER, 0); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
      $rawdata=curl_exec($ch); 
      curl_close($ch); 
      $name = uniqid('captcha_'); 
      $fp = fopen("$name.jpg",'w'); 
      fwrite($fp, $rawdata); 
      fclose($fp); 
     //ese cont hay que guardarlo en BD y generar otro para la imagen 
     //$picturename = $this->grab_image1($img); 
     $picturename = $name.".jpg"; 
     $picture = array('name' => $picturename, 'hash' => $cont); 
     return $picture; 
    } 
} 

?> 

所以你只能做這樣的判斷捲曲類:

include 'class-Curl.php'; 

//Pedir un nuevo captcha con una nueva cookie 
$url = 'http://lok.myvnc.com/insertar-anuncio.html'; 
//Crear el objeto Curl 
$captcha = new cURL($url); 
//Capturar el codigo funte de la pagina 
$source = $captcha->get(); 
//Parsear el codigo javascripts del captcha y bajarla al disco 
$captchaimg = $captcha->saveCaptcha($source); 
//Guardar en Base de Datos las variables ID, picturename, picturehash, cookie 


var_dump($captchaimg); 
?> 
<IMG src="<?php echo $_SERVER['DOCUMENT_ROOT']."/sms/".$captchaimg['name'] ?>"> 
+0

你能解釋這碼多一點?什麼進來,這個功能是什麼? – angabriel 2014-01-07 20:51:04

+0

我希望你能理解我的變化,對我來說工作正常。我在數據庫中保存了cookie,captcha和de captcha哈希。因此,稍後在帖子($ data)中發佈三件事。 – n3omaster 2014-01-10 15:47:52