Guzzle Http客户端和LinkedIn中的授权


Guzzle Http Client and authorization in LinkedIn

我尝试模拟授权LinkedIn web浏览器(PHP)。我使用Guzzle Http客户端。

这是授权代码的一部分:

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

请求成功,返回200代码,但我没有授权。谁能面对类似的任务,或者在代码中遗漏了什么。如果有任何建议,我将不胜感激。

我认为问题在于CSRF保护和其他隐藏参数。LinkedIn和其他网站一样,通常在所有情况下都会返回200 OK,即使是错误,并在生成的HTML中描述细节。

在您的情况下,最好使用网刮器,如Goutte。它用浏览器模拟用户,所以你不需要担心很多事情(比如CSRF保护和其他隐藏字段)。例子可以在主页上找到,试试这样的东西:

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

你可以将它与Guzzle一起用作驱动程序,但有些网站可能需要JavaScript(我不确定亚马逊)。然后你必须去一个真正的浏览器或PhantomJS(一种无头Chrome)。