title: Yii2 cUrl Bad Request (#400)
body: <p>While trying to use cUrl with the Post method in Yii 2, I receive a 400 error code.</p>
  
  <pre><code>Bad Request (#400)
  Unable to verify your data submission.
  The above error occurred while the Web server was processing your request.
  
  Please contact us if you think this is a server error. Thank you.
  </code></pre>
  
  <p>This is my code, where i instantiate the CurlTool class:</p>
  
  <pre><code>public function actionSend() {
          $model = new \app\models\Licitatie;
          if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
              $curl_tool = new \common\components\CurlTool();
              $result = $curl_tool-&gt;fetchContent('http://www.william.ro/licitatia_bursa/frontend/web/index.php/organizator/licitatie/evrika', $model-&gt;attributes);
              print_r($result);
          }
      }
  
      public function actionEvrika() {
          return json_encode(
                  array(
                      'a' =&gt; 'b',
                  )
          );
      }
  </code></pre>
  
  <p>this is the curltool class code:</p>
  
  <pre><code>&lt;?php
  
  namespace common\components;
  
  class CurlTool {
  
      public static $userAgents = array(
          'FireFox3' =&gt; 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
          'GoogleBot' =&gt; 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
          'IE7' =&gt; 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
          'Netscape' =&gt; 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
          'Opera' =&gt; 'Opera/9.25 (Windows NT 6.0; U; en)'
      );
      public static $options = array(
          CURLOPT_USERAGENT =&gt; 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
          CURLOPT_AUTOREFERER =&gt; true,
          CURLOPT_FOLLOWLOCATION =&gt; false,
          CURLOPT_RETURNTRANSFER =&gt; true,
          CURLOPT_FRESH_CONNECT =&gt; true,
          CURLOPT_COOKIEJAR =&gt; "cookies.txt",
          CURLOPT_COOKIEFILE =&gt; "cookies.txt",
          CURLOPT_SSL_VERIFYPEER =&gt; false,
              //CURLOPT_COOKIESESSION =&gt; false,
      );
      private static $proxyServers = array();
      private static $proxyCount = 0;
      private static $currentProxyIndex = 0;
      public static $getinfo;
  
      public static function addProxyServer($url) {
          self::$proxyServers[] = $url;
          ++self::$proxyCount;
      }
  
      public static function fetchContent($url, $fields = null, $verbose = false) {
          //print '*'.$fields.'*';
          if (($curl = curl_init($url)) == false) {
              throw new Exception("curl_init error for url $url.");
          }
          if (self::$proxyCount &gt; 0) {
              $proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
              curl_setopt($curl, CURLOPT_PROXY, $proxy);
              if ($verbose === true) {
                  echo "Reading $url [Proxy: $proxy] ... ";
              }
          } else if ($verbose === true) {
              echo "Reading $url ... ";
          }
          //$verbose=TRUE;
          //print_r($fields);
  //        debug_print_backtrace();
  //url-ify the data for the POST
          $fields_string = '';
          if (is_array($fields))
              foreach ($fields as $key =&gt; $value) {
                  if (empty($key))
                      continue;
                  $fields_string .= $key . '=' . urlencode($value) . '&amp;';
                  if ($verbose === true) {
                      echo $key . ": " . $value;
                  }
              }
          rtrim($fields_string, '&amp;');
  
          if (count($fields) &gt; 0) {
              curl_setopt($curl, CURLOPT_POST, count($fields));
              curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
          }
  
          if ($verbose === true) {
              echo "Fields string $fields_string ... ";
          }
  
          curl_setopt_array($curl, self::$options);
  
          $content = curl_exec($curl);
          self::$getinfo = curl_getinfo($curl);
          if ($content === false) {
              throw new Exception("curl_exec error for url $url " . curl_error($curl));
          }
  
          curl_close($curl);
          if ($verbose === true) {
              echo "Done.\n";
          }
          $content = preg_replace('#\n+#', ' ', $content);
          $content = preg_replace('#\s+#', ' ', $content);
  
          return $content;
      }
  
  }
  </code></pre>