pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

PHP pastebin - collaborative debugging tool View Help


Posted by Liger on Sat 18 Oct 07:12
report abuse | View followups from anthony, gogo and Anonymous | download | new post

  1. <?php
  2.         //
  3.         // Configuration
  4.         //
  5.         //Your API key
  6.         $api_key = '123-123-123-123-123';
  7.  
  8.         //Path to captcha image
  9.         $file = getcwd() . '/captcha.jpg';
  10.  
  11.         //Url to CaptchaKiller API
  12.         $url = 'http://www.captchakiller.com/api.php';
  13.  
  14.        
  15.         //
  16.         // Functions
  17.         //
  18.         function upload_captcha($api_key, $file)
  19.         {
  20.                 global $crl;
  21.  
  22.                 $postdata = array(
  23.                                 'method'        => 'upload_captcha',
  24.                                 'api_key'       => $api_key,
  25.                                 'file'          => "@$file",
  26.                                 'expire'        => '300',
  27.                                 'rights'        => 'false'
  28.                                 );
  29.  
  30.                 curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata);
  31.  
  32.                 $result = curl_exec($crl);
  33.                 $headers = curl_getinfo($crl);
  34.  
  35.                 if ($headers['header_size'] == 0)
  36.                 {
  37.                         curl_close($crl);
  38.                         die('Upload Transaction failed');
  39.                 }
  40.                 else
  41.                 {
  42.                         echo 'Successfully uploaded captcha<br />';
  43.                 }
  44.  
  45.                 //Parse captcha ID
  46.                 $pos = strpos($result, 'SUCCESS: captcha_id=');
  47.                 $captcha_id = 0;
  48.  
  49.                 if($pos !== false)
  50.                 {
  51.                         $pattern = '([\w\-]+)';
  52.                         $matches = array();
  53.  
  54.                         if(preg_match($pattern, substr($result, 20), $matches)) //20 = length of text before the ID
  55.                         {
  56.                                 $captcha_id = $matches[0];
  57.                                 echo 'Captcha ID: ' . $captcha_id . '<br />';
  58.                         }
  59.                         else
  60.                         {
  61.                                 echo 'Could not find captcha id in: \'' . $result . '\'<br />';
  62.                         }
  63.                 }
  64.                 else
  65.                 {
  66.                         echo $result;
  67.                 }
  68.                
  69.                 return $captcha_id;
  70.         }
  71.  
  72.         function get_result($api_key, $captcha_id)
  73.         {
  74.                 global $crl;
  75.  
  76.                 //Send request for captcha result
  77.                 $postdata = array(
  78.                                 'method'                => 'get_result',
  79.                                 'api_key'               => $api_key,
  80.                                 'captcha_id'    => $captcha_id
  81.                                 );
  82.  
  83.                 curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata);
  84.  
  85.                 $result = curl_exec($crl);
  86.                 $headers = curl_getinfo($crl);
  87.  
  88.                 if ($headers['header_size'] == 0)
  89.                 {
  90.                         curl_close($crl);
  91.                         die('Get Result transaction failed');
  92.                 }
  93.                
  94.                 return $result;
  95.         }
  96.  
  97.         function process_captcha($api_key, $file, $url)
  98.         {
  99.                 $captcha_id = upload_captcha($api_key, $file, $url);
  100.                 $captcha_result = '';
  101.                
  102.                 if($captcha_id != 0)
  103.                 {
  104.                         echo 'Getting result';
  105.  
  106.                         for($i = 0; $i < 20; $i++)
  107.                         {
  108.                                 //Wait a few seconds before sending the next request
  109.                                 sleep(5);
  110.  
  111.                                 //Get the result
  112.                                 $result = get_result($api_key, $captcha_id);
  113.  
  114.                                 //Parse the result
  115.                                 $pos_success = strpos($result, 'SUCCESS: captcha_result="');
  116.                                 $pos_wait = strpos($result, 'WAIT: check back later');
  117.  
  118.                                 if($pos_success !== false)
  119.                                 {
  120.                                         $pattern = '(.*)';
  121.                                         $matches = array();
  122.  
  123.                                         if(preg_match($pattern, substr($result, 25), $matches)) //24 = length of text before the result
  124.                                         {
  125.                                                 $captcha_result = substr($matches[0], 0, -1);   //Trim off the trailing "
  126.                                                 echo '<br />Captcha Result: ' . $captcha_result . '<br />';
  127.                                         }
  128.                                         else
  129.                                         {
  130.                                                 echo '<br />Could not find captcha result in: \'' . $result . '\'<br />';
  131.                                         }
  132.                                         break;
  133.                                 }
  134.                                 else if($pos_wait !== false)
  135.                                 {
  136.                                         //WAIT found, try again
  137.                                         echo '.';
  138.                                         continue;
  139.                                 }
  140.                                 else
  141.                                 {
  142.                                         //FAILURE found
  143.                                         echo '<br />' . $result;
  144.                                         break;
  145.                                 }
  146.  
  147.                                 if($i == 19)
  148.                                 {
  149.                                         echo '<br />20 attempts have passed and still received WAIT<br />';
  150.                                 }
  151.                         }
  152.                 }
  153.                
  154.                 return $captcha_result;
  155.         }
  156.        
  157.         //
  158.         // Main program
  159.         //
  160.         // Start the cURL session
  161.         $crl = curl_init();
  162.         curl_setopt($crl, CURLOPT_URL, $url);
  163.         curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
  164.        
  165.         $captcha = process_captcha($api_key, $file, $url);
  166.  
  167.         //Close the cURL session
  168.         curl_close($crl);
  169. ?>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post