Guzzle Http Client 和 LinkedIn 授权
Guzzle Http Client and authorization in LinkedIn
我尝试模拟授权 LinkedIn 网络浏览器 (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;
}
}
请求成功,returns一个200码,但是我没有授权。
谁能面对类似的任务,或者在代码中遗漏了什么。如果有任何建议,我将不胜感激。
我认为问题出在 CSRF 保护和其他隐藏参数上。与其他网站一样,LinkedIn 对于所有情况通常 returns 200 OK,即使是错误,并在结果 HTML.
中描述详细信息
在你的情况下,最好使用网络 scraper,例如 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,尝试这样的事情:
$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)。
我尝试模拟授权 LinkedIn 网络浏览器 (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;
}
}
请求成功,returns一个200码,但是我没有授权。 谁能面对类似的任务,或者在代码中遗漏了什么。如果有任何建议,我将不胜感激。
我认为问题出在 CSRF 保护和其他隐藏参数上。与其他网站一样,LinkedIn 对于所有情况通常 returns 200 OK,即使是错误,并在结果 HTML.
中描述详细信息在你的情况下,最好使用网络 scraper,例如 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,尝试这样的事情:
$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)。