[ask] how to get captcha with PHP cURL?

Discussion in 'PHP' started by ghprod, Mar 26, 2010.

  1. #1
    Hi all,

    i'm doing my own project where this script fetch content from remote server, get captcha, display to user to fill in then submit it.

    the problem is i can't get captcha for this site _http://abadijayaiklan.co.cc/pasang-iklan/ (remove underscore in front of url), its seems like their captcha using more complex session :(

    btw this is my code, i've try it with other site and works perfectly, just can't get it work for site above.

    
    <?php
    
    function open($url)
    {
    	$ch = curl_init();
    	
    	curl_setopt($ch, CURLOPT_URL,$url);  
    	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_COOKIE, 1);
    	curl_setopt($ch, CURLOPT_COOKIEJAR, "1");
    	curl_setopt($ch, CURLOPT_COOKIEFILE, "1");
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);    
        $result = curl_exec($ch);  
        curl_close($ch);
    	
    	return $result;
    }
    
    function between($string, $start, $end)
    {
    	$out	= explode($start, $string);
    	
    	if(isset($out[1]))
    	{
    		$string	= explode($end, $out[1]);
    		return $string[0];
    	}
    	
    	return '';
    }
    
    function get_captcha()
    {
    	$url	= 'http://abadijayaiklan.co.cc/pasang-iklan/';
    	
    	$open	= open($url);
    	
    	$code	= between($open, '<img src="http://abadijayaiklan.co.cc/captcha.php', '">');
    	
    	return 'http://abadijayaiklan.co.cc/captcha.php' . $code;
    }
    
    ?>
    
    <form action="" method="post">
    <img src="<?php echo get_captcha(); ?>" border="0" /><br />
    <input type="text" name="code" /><br />
    <input type="submit" value="Submit" />
    </form>
    
    PHP:
    or maybe i must save captcha first? but how to do it?

    i hope someone can help me out :)

    Thanks and regards

    Please not this is not for SPAM
     
    ghprod, Mar 26, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Try:

    <?php
    
    function open($url)
    {
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL,$url);  
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "1");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "1");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://abadijayaiklan.co.cc/pasang-iklan/');    
        $result = curl_exec($ch);  
        curl_close($ch);
        
        return $result;
    }
    
    preg_match('~http://abadijayaiklan\.co\.cc/captcha\.php\?VID=([0-9a-z]+)~', open('http://abadijayaiklan.co.cc/pasang-iklan/'), $a);
    ?>
    
    
    <form action="" method="post">
    <img src="http://abadijayaiklan.co.cc/captcha.php?VID=<?php echo $a[1]; ?>" border="0" /><br />
    <input type="text" name="code" /><br />
    <input type="submit" value="Submit" />
    </form>
    PHP:
     
    danx10, Mar 27, 2010 IP
  3. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #3
    Hi danx10, thanks for your reply,

    anyway, your code and mine success to get URL of captcha, but still captcha can't load, bcause cookies was changed, or anything else.
    i think we must save cookies temp and load captcha with cookies inside, but how?

    thanks
     
    ghprod, Mar 27, 2010 IP
  4. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    
     $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);  
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie.txt");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_REFERER, 'http://abadijayaiklan.co.cc/pasang-iklan/');    
        $result = curl_exec($ch);  
        curl_close($ch);
        
        return $result;
    
    PHP:
    di kaskus mang ga da yg bsa? :D

    Don't forget to include the cookies, when sending postfields
     
    guardian999, Mar 28, 2010 IP
  5. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #5
    Blm coba d kkaskus, btw aku dah pernah pk yg kita save cookies ke file txt juga tapi tetep g bsa :(

    pusing juga euy ..


    english : Thanks bro, i've ever try similiar script but not working :(
     
    ghprod, Mar 28, 2010 IP