如何在 Guzzle 通话中通过登录屏幕
Posted
技术标签:
【中文标题】如何在 Guzzle 通话中通过登录屏幕【英文标题】:How to get past login screen on Guzzle call 【发布时间】:2014-09-25 05:33:24 【问题描述】:我必须使用 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 所需的部分信息。
【问题讨论】:
【参考方案1】:curl -F
提交 POST 请求而不是 GET 请求。所以你需要相应地修改你的代码,比如
$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()
关联的身份验证cookie。 http://guzzle.readthedocs.org/en/latest/clients.html#cookies
$response2 = $client->get('http://website.com/otherpage/', ['cookies' => true]);
【讨论】:
使用你的建议我仍然只得到登录页面。不知道我还能尝试什么。 为了确定,您可以使用给定的命令成功地对 url 执行 cURL 请求,对吗? 我想是的,当我回显 $xml 变量时,页面会出现,但只会出现在登录屏幕上。它需要调出页面,但需要成功登录。然后我可以继续我需要做的事情,即通过 xml 文件发送 对不起,我指的是你帖子中的命令。curl -F "action=login" -F "username=<username>" -F "password=<password>" http://website.com/page/login/
会生成登录页面还是正确的页面?
@RoydonD'Souza 看看***.com/a/6578969 和***.com/a/3577662【参考方案2】:
我无法让 @JeremiahWinsley 的答案在 Guzzle 的较新版本上工作,因此我更新了他们的代码以从 Guzzle 5.x 开始工作。
需要进行三项重大更改
使用form_params
而不是body
来防止错误“将“body”请求选项作为数组传递以发送POST 请求已被弃用。”
更改 cookie 以使用 CookieJar
对象
使用->getBody()->getContents()
获取body of the request
这是更新后的代码:
$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;
要在以后的请求中继续使用 cookie,请将 cookieJar
传递给请求:
$response2 = $client->get('http://website.com/otherpage/', ['cookies' => $cookieJar]);
【讨论】:
【参考方案3】:我在获取 @JeremiahWinsley 和 @Samsquanch 的答案来开发新版本的 Guzzle 时遇到了麻烦。所以我已经更新了代码以从 Guzzle 6.x 开始工作。
Guzzle 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();
【讨论】:
这个答案是更新的并且按预期工作。我在功能测试中使用了类似的东西。 2021 年工作以上是关于如何在 Guzzle 通话中通过登录屏幕的主要内容,如果未能解决你的问题,请参考以下文章
如何在 jhipster 中通过 OAuth2 成功登录时执行操作
如何在 Spring Security 中通过 jdbc 身份验证使用自定义登录页面