title: How to generate a Google ReCaptcha V2 secure token with PHP?
body: <p>I'm trying to generate a secure token for ReCaptcha V2, as described here:
  <a href="https://developers.google.com/recaptcha/docs/secure_token">https://developers.google.com/recaptcha/docs/secure_token</a></p>
  
  <p>Unfortunately, my generated stoken isn't valid and I can't find a way to check why it doesn't work. There is a working Java example (<a href="https://github.com/google/recaptcha-java/blob/master/appengine/src/main/java/com/google/recaptcha/STokenUtils.java">STokenUtils.java</a>),
  but I find myself unable to translate it to PHP.</p>
  
  <pre><code>public static function generateSecurityToken($secretKey){
      $stoken = array(
          'session_id' =&gt; session_id(),
          'ts_ms' =&gt; round(microtime(true)*1000)
      );
      $secretKey = self::pkcs5_pad(hash('sha1', $secretKey), 16);
      $stoken_json = json_encode($stoken);
      $stoken_crypt = self::encrypt(self::pkcs5_pad($stoken_json, 16), $secretKey);
      return $stoken_crypt;
  }
  
  public static function encrypt($sStr, $sKey) {
      return base64_encode(
          mcrypt_encrypt(
              MCRYPT_RIJNDAEL_128, 
              base64_decode($sKey),
              $sStr,
              MCRYPT_MODE_ECB
          )
      );
  }
  
  public static function pkcs5_pad ($text, $blocksize) { 
      $pad = $blocksize - (strlen($text) % $blocksize); 
      return $text . str_repeat(chr($pad), $pad); 
  }
  </code></pre>
  
  <p>Can anybody provide a working PHP example or point out any obvious mistakes in my code?</p>