문제

cURL을 사용하여 외부 웹사이트에 정보를 보내야 합니다.Laravel 애플리케이션에 Guzzle을 설정했습니다.기본 사항은 설정했지만 웹사이트 설명서에 따르면 사용자 이름과 비밀번호에 필요한 작업이 있습니다.로그인하고 액세스하는 데 필요한 자격 증명과 함께 '작업'을 어떻게 전달합니까?

웹 사이트에는 다음과 같이 명시되어 있습니다.

curl [-k] –dump-header <header_file> -F “action=login” -F “username=<username>” -F “password=<password>” https://<website_URL>

내 컨트롤러:

    $client = new \GuzzleHttp\Client();

    $response = $client->get('http://website.com/page/login/', array(
        'auth' => array('username', 'password')
    ));

    $xml = $response;
    echo $xml;

웹사이트가 로드됩니다. echo, 하지만 로그인 화면만 표시됩니다.cURL에 필요한 정보 부분에 접근하려면 로그인 화면(로그인 성공)을 우회하려면 해당 자격 증명이 필요합니다.

도움이 되었습니까?

해결책

curl -F GET 요청 대신 POST 요청을 제출합니다.따라서 그에 따라 코드를 수정해야 합니다.

$client = new \GuzzleHttp\Client();

$response = $client->post('http://website.com/page/login/', [
    'body' => [
        'username' => $username,
        'password' => $password,
        'action' => 'login'
    ],
    'cookies' => true
]
);

$xml = $response;
echo $xml;

보다 http://guzzle.readthedocs.org/en/latest/quickstart.html#post-requests, http://curl.haxx.se/docs/manpage.html#-F

편집하다:

그냥 추가하세요 ['cookies' => true] 이와 관련된 인증 쿠키를 사용하기 위한 요청 GuzzleHttp\Client(). http://guzzle.readthedocs.org/en/latest/clients.html#cookies

$response2 = $client->get('http://website.com/otherpage/', ['cookies' => true]);

다른 팁

최신 버전의 Guzzle에서 작동하도록 @JeremiahWinsley의 답변을 얻는 데 문제가 있어서 Guzzle 5.x부터 작동하도록 코드를 업데이트했습니다.

세 가지 주요 변경이 필요합니다

  • 사용 form_params 대신에 body "POST 요청을 보내기 위해 배열로 "body" 요청 옵션을 전달하는 것은 더 이상 사용되지 않습니다." 오류를 방지하기 위해.
  • 쿠키를 사용하도록 변경 CookieJar 물체
  • 사용 ->getBody()->getContents() 얻기 위해 요청 본문

업데이트된 코드는 다음과 같습니다.

$client = new \GuzzleHttp\Client();
$cookieJar = new \GuzzleHttp\Cookie\CookieJar();

$response = $client->post('http://website.com/page/login/', [
    'form_params' => [
        'username' => $username,
        'password' => $password,
        'action' => 'login'
    ],
    'cookies' => $cookieJar
]
);

$xml = $response->getBody()->getContents();
echo $xml;

향후 요청에서 쿠키를 계속 사용하려면 cookieJar 요청에:

$response2 = $client->get('http://website.com/otherpage/', ['cookies' => $cookieJar]);

@JeremiahWinsley와 @Samsquanch의 답변을 최신 버전의 Guzzle에서 작업하는 데 문제가 있었습니다.그래서 Guzzle 6.x부터 작동하도록 코드를 업데이트했습니다.

6.x를 씹어 먹습니다.서류: http://docs.guzzlephp.org/en/stable/index.html

업데이트된 코드는 다음과 같습니다.

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;

try {
        $client = new Client();
        $cookieJar = new CookieJar();

        $response = $client->request('POST', 'http://website.com/page/login/', [
            'form_params' => [
                'username' => 'test@example.com',
                'password' => '123456'
            ],
            'cookies' => $cookieJar
        ]);

        $response2 = $client->request('GET', 'http://website.com/otherpage/', [
            'cookies' => $cookieJar
        ]);

        if ($response2->getStatusCode() == 200) {
            return $response2->getBody()->getContents();
        } else {
            return "Oops!";
        }
    } catch (\Exception $exception) {
        return 'Caught exception: ', $exception->getMessage();
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top