加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Yii2 cUrl错误请求(#400)

发布时间:2020-12-13 18:13:06 所属栏目:PHP教程 来源:网络整理
导读:尝试在Yii 2中使用带有Post方法的cUrl时,我收到400错误代码. 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 erro
尝试在Yii 2中使用带有Post方法的cUrl时,我收到400错误代码.
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.

这是我的代码,我在其中实例化CurlTool类:

public function actionSend() {
        $model = new appmodelsLicitatie;
        if ($model->load(Yii::$app->request->post())) {
            $curl_tool = new commoncomponentsCurlTool();
            $result = $curl_tool->fetchContent('http://www.william.ro/licitatia_bursa/frontend/web/index.php/organizator/licitatie/evrika',$model->attributes);
            print_r($result);
        }
    }

    public function actionEvrika() {
        return json_encode(
                array(
                    'a' => 'b',)
        );
    }

这是curltool类代码:

<?php

namespace commoncomponents;

class CurlTool {

    public static $userAgents = array(
        'FireFox3' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0','GoogleBot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)','IE7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)','Netscape' => 'Mozilla/4.8 [en] (Windows NT 6.0; U)','Opera' => 'Opera/9.25 (Windows NT 6.0; U; en)'
    );
    public static $options = array(
        CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',CURLOPT_AUTOREFERER => true,CURLOPT_FOLLOWLOCATION => false,CURLOPT_RETURNTRANSFER => true,CURLOPT_FRESH_CONNECT => true,CURLOPT_COOKIEJAR => "cookies.txt",CURLOPT_COOKIEFILE => "cookies.txt",CURLOPT_SSL_VERIFYPEER => false,//CURLOPT_COOKIESESSION => 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 > 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 => $value) {
                if (empty($key))
                    continue;
                $fields_string .= $key . '=' . urlencode($value) . '&';
                if ($verbose === true) {
                    echo $key . ": " . $value;
                }
            }
        rtrim($fields_string,'&');

        if (count($fields) > 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;
    }

}
class Controller extends yiibaseController
{
    /**
     * @var boolean whether to enable CSRF validation for the actions in this controller.
     * CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true.
     */
    public $enableCsrfValidation = false; <- set this to false

...

小心,我发现如果在动作本身中使用此设置,它可能会失败;

在行动之前可能会失败;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读