Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get past login screen on Guzzle call

I have to send information to an external website using cURL. I set up Guzzle on my Laravel application. I have the basics set up, but according to the documentation of the website, there is an action that's required for the username and password. How can I pass the 'action' along with the credentials needed to log in and get access?

The website states:

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

My controller:

    $client = new \GuzzleHttp\Client();

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

    $xml = $response;
    echo $xml;

The website will load on the echo, but it will only pull up the login screen. I need those credentials to bypass the login screen (with a successful login) to get to the portion of information I need for cURL.

like image 234
Lynx Avatar asked Aug 01 '14 23:08

Lynx


People also ask

How do I get past the header in guzzle?

// Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ] ]); Headers may be added as default options when creating a client.

How do I get response guzzle?

As described earlier, you can get the body of a response using the getBody() method. Guzzle uses the json_decode() method of PHP and uses arrays rather than stdClass objects for objects. You can use the xml() method when working with XML data.

How do I log guzzle request?

Simple usage. use GuzzleLogMiddleware\LogMiddleware; use GuzzleHttp\HandlerStack; $logger = new Logger(); //A new PSR-3 Logger like Monolog $stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it.

What is guzzle exception?

A GuzzleHttp\Exception\ConnectException exception is thrown in the event of a networking error. This exception extends from GuzzleHttp\Exception\TransferException . A GuzzleHttp\Exception\ClientException is thrown for 400 level errors if the http_errors request option is set to true.


2 Answers

I was having trouble getting @JeremiahWinsley's and @Samsquanch's answer to work on newer version of Guzzle. So I've updated the code to work as of Guzzle 6.x.

Guzzle 6.x. documents: http://docs.guzzlephp.org/en/stable/index.html

Here is the updated code:

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' => '[email protected]',
                '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();
    }
like image 137
Md Rasel Ahmed Avatar answered Oct 22 '22 13:10

Md Rasel Ahmed


I was having trouble getting @JeremiahWinsley's answer to work on newer version of Guzzle so I've updated their code to work as of Guzzle 5.x.

Three major changes are required

  • Using form_params instead of body to prevent the error "Passing in the "body" request option as an array to send a POST request has been deprecated."
  • Changing the cookies to use the CookieJar object
  • Use ->getBody()->getContents() to get the body of the request

Here is the updated code:

$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;

And to continue using cookies in future requests, pass in the cookieJar to the request:

$response2 = $client->get('http://website.com/otherpage/', ['cookies' => $cookieJar]);
like image 20
Samsquanch Avatar answered Oct 22 '22 11:10

Samsquanch