Php 我无法通过curl登录到该站点

Php 我无法通过curl登录到该站点,php,parsing,curl,php-curl,Php,Parsing,Curl,Php Curl,我不明白我做错了什么。有一个服务站点:。带标记的常用形式。我敲了敲页面,保存cookies,从表单中取出代币。我从表单数据发送到地址的下一个请求。带着饼干和代币。我得到一张图401 我检查了一切,我发送了相同的数据,相同的标题。告诉我,我错过了什么 $receipt = new Receipt; $receipt->get_scrf(); echo $receipt->get_auth(); Class Receipt { private $scrf; priv

我不明白我做错了什么。有一个服务站点:。带标记的常用形式。我敲了敲页面,保存cookies,从表单中取出代币。我从表单数据发送到地址的下一个请求。带着饼干和代币。我得到一张图401

我检查了一切,我发送了相同的数据,相同的标题。告诉我,我错过了什么

$receipt = new Receipt;

$receipt->get_scrf();
echo $receipt->get_auth();

Class Receipt {
    private $scrf;

    private $login_page_url = 'https://demo.moneta.ru/login.htm';
    private $login_action_url = 'https://demo.moneta.ru/login';

    private $login = 'LOGIN';
    private $password = "PASSWORD";

    public function get_auth(){
    $headers = [
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cache-Control: max-age=0',
        'Origin: https://demo.moneta.ru',
        'Referer: https://demo.moneta.ru/login.htm',
        'Sec-Fetch-Dest: document',
        'Sec-Fetch-Mode: navigate',
        'Sec-Fetch-Site: same-origin',
        'Sec-Fetch-User: ?1',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
        'Content-Type: application/x-www-form-urlencoded',
        'Connection: keep-alive'
    ];

        $post = [
            'state' => $this->scrf,
            'target' => 'desktop',
            'login' => $this->login,
            'password' => $this->password
        ];

        return $this->get_page($this->login_action_url, $post, $headers);
    }

    public function get_scrf(){
        $page_html = $this->get_page($this->login_page_url);

        preg_match('/\<input\ type\=\"hidden\"\ name\=\"state\"\ value\=\"([0-9a-z\-]+)\"\>/', $page_html, $matches, PREG_OFFSET_CAPTURE);

        if(isset($matches[1][0])){
            $this->scrf = $matches[1][0];
            return $matches[1][0];
        }
        return null;
    }

    private function get_page($url, $post=null, $headers=null){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEFILE,  dirname(__FILE__).'/cookie.txt');
        curl_setopt($ch, CURLOPT_POST, $post!==null );

        if($headers){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        if($post){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
$receive=新收据;
$receive->get_scrf();
echo$receipt->get_auth();
班级收据{
私人$scrf;
私有$login\u页面\u url='0https://demo.moneta.ru/login.htm';
私有$login\u action\u url='1https://demo.moneta.ru/login';
private$login='login';
private$password=“password”;
公共函数get_auth(){
$headers=[
'接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed exchange;v=b3;q=0.9',
'接受编码:gzip,deflate,br',
‘接受语言:ru-ru,ru;q=0.9,en-US;q=0.8,en;q=0.7’,
'缓存控制:最大年龄=0',
“来源:https://demo.moneta.ru',
“推荐人:https://demo.moneta.ru/login.htm',
“Sec Fetch Dest:document”,
'秒提取模式:导航',
“Sec获取站点:相同来源”,
'秒获取用户:?1',
'升级不安全的请求:1',
'用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/80.0.3987.132 Safari/537.36',
'内容类型:application/x-www-form-urlencoded',
“连接:保持活动”
];
$post=[
'state'=>this->scrf,
'目标'=>'桌面',
'login'=>this->login,
“密码”=>$this->password
];
返回$this->get\u页面($this->login\u action\u url,$post,$headers);
}
公共功能get_scrf(){
$page\u html=$this->get\u page($this->login\u page\u url);
预匹配(“/\/”,$page\u html,$matches,预偏移捕获);
如果(isset($matches[1][0])){
$this->scrf=$matches[1][0];
返回$matches[1][0];
}
返回null;
}
私有函数get_page($url,$post=null,$headers=null){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_COOKIEJAR,dirname(_文件).'/cookie.txt');
curl_setopt($ch,CURLOPT_COOKIEFILE,dirname(uuu FILE_uu)。'/cookie.txt');
curl_setopt($ch,CURLOPT_POST,$POST!==null);
如果($headers){
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
}
如果有的话(员额){
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
}
$data=curl\u exec($ch);
卷曲关闭($ch);
返回$data;
}
}
你有

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
其中,
$post
是一个关联数组

如果
value
是一个数组,则内容类型标题将设置为多部分/表单数据

这不是表单的行为,与显式设置的内容类型标题冲突:

'Content-Type: application/x-www-form-urlencoded'

尝试将POST数据作为URL编码字符串传递给
curl\u setopt($ch,CURLOPT\u POSTFIELDS,…

你只需要


您是否尝试与远程站点联系以寻求帮助?能否提供一些测试凭据?
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $post ) );