简体   繁体   中英

Guzzle Http Client and authorization in LinkedIn

I try to simulate the authorization LinkedIn web browser (PHP). I use Guzzle Http Client.

Here is part of the authorization code:

use GuzzleHttp\Client as LinkedinClient;
use PHPHtmlParser\Dom as Parser;

public function authLinkedin()
{
    $client = new LinkedinClient(['base_url' => 'https://www.linkedin.com']);

    try {
        $postData = [
            'session_key'       => 'My_email',
            'session_password'  => 'My_password',
            'action' => 'login'
            ];

        $request = $client->createRequest('POST', '/uas/login', ['body' => $postData, 'cookies' => true]);

        $response = $client->send($request);

        if ($response->getStatusCode() === 200) {
            $parser = new Parser();
            $parser->load($client->get('https://www.linkedin.com/', ['cookies' => true])->getBody());

            return $parser;
        } else {
            Log::store("Authorization error", Log::TYPE_ERROR, $request->getStatusCode());
            return null;
        }
        return $request;
    } catch (Exception $ex) {
        Log::store("Failure get followers", Log::TYPE_ERROR, $ex->getMessage());

        return null;
    }
}

The request is successful, returns a 200 code, but I did not authorize. Who can faced with a similar task, or in the code have missed something. I would appreciate any advice.

I think that the issue is with CSRF protection and other hidden parameters. LinkedIn, as other sites, usually returns 200 OK for all situations, even for an error, and describes details in resulting HTML.

In your case it's better to use a web scraper , like Goutte . It emulates a user with a browser, so you don't need to worry about many things (like CSRF protection and other hidden fields). Examples can be found on the main pages , try something like this:

$crawler = $client->request('GET', 'https://www.linkedin.com');
$form = $crawler->selectButton('Sign In')->form();
$crawler = $client->submit($form, array(
    'login' => 'My_email',
    'password' => 'My_password'
));

You can use it with Guzzle as a driver, but some sites might require JavaScript (I'm not sure about Amazon). Then you have to go to a real browser or PhantomJS (a kind of headless Chrome).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM