문제

내 웹 사이트에 로그인 할 간단한 코드를 만들었습니다.

private function request($url, $reset_cookies, $post_data)
  {

    $options = array(
      CURLOPT_URL               => $url,
      CURLOPT_RETURNTRANSFER    => 1,
      CURLOPT_HEADER            => 0,
      CURLOPT_FAILONERROR       => 1,
      CURLOPT_USERAGENT         => $this->user_agent,
      CURLOPT_CONNECTTIMEOUT    => 30,
      CURLOPT_TIMEOUT           => 30,
      CURLOPT_SSL_VERIFYPEER    => 0,   
      CURLOPT_FOLLOWLOCATION    => 1,
      CURLOPT_MAXREDIRS         => 10,
      CURLOPT_AUTOREFERER       => 1,
      CURLOPT_COOKIESESSION     => $reset_cookies ? 1 : 0,
      CURLOPT_COOKIEJAR         => $this->session_id,
      CURLOPT_COOKIEFILE        => $this->session_id,
    );

    // Add POST data
    if (isset($post_data))
    {
      $options[CURLOPT_CUSTOMREQUEST] = 'POST';
      $options[CURLOPT_POST]          = 1;
      $options[CURLOPT_POSTFIELDS]    = http_build_query($post_data);
    }

    // Attach options
    curl_setopt_array($this->curl, $options);

    // Execute the request and read the response
    $content = curl_exec($this->curl);

    // Handle any error
    if (curl_errno($this->curl)) throw new Exception(curl_error($this->curl));

    return $content;
  }
.

기본적으로 내 로그인 시스템의 정상적인 단계가 다음과 같습니다.

  1. 보안 토큰을 검색하고 쿠키 세션을 초기화하라는 GET 요청을 보냅니다.(완료 및 작업)

    $security_token = $this->browser->request('https://mysite.com/login', true, null);
    
  2. 사용자 이름, 암호 및 보안 토큰으로 POST 요청을 보냅니다.

    $postdata = array(
        'login' => $login,
        'password' => $password,
        '__RequestVerificationToken' => $security_token,
    );
    $login_page = $this->browser->request('https://mysite.com/login', false, $postdata);
    
  3. 단계 # 2에서는 콘텐츠 길이가 필요하다는 411 오류가 발생했습니다.

    뭔가 잘못했거나 매개 변수를 설정하는 것을 잊었습니까?

도움이 되었습니까?

해결책

해결책은이 라인을 제거하는 것이 었습니다

$options[CURLOPT_CUSTOMREQUEST] = 'POST';
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top