laravel 如何在 Guzzle 通话中跳过登录屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25089960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get past login screen on Guzzle call
提问by Lynx
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?
我必须使用 cURL 将信息发送到外部网站。我在 Laravel 应用程序上设置了 Guzzle。我已经设置了基础知识,但是根据网站的文档,需要对用户名和密码进行操作。如何传递“操作”以及登录和访问所需的凭据?
The website states:
该网站指出:
curl [-k] –dump-header <header_file> -F “action=login” -F “username=<username>” -F “password=<password>” https://<website_URL>
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.
该网站将加载到 上echo
,但只会显示登录屏幕。我需要这些凭据来绕过登录屏幕(成功登录)以获取 cURL 所需的部分信息。
回答by Jeremiah Winsley
curl -F
submits a POST request instead of a GET request. So you'll need to modify your code accordingly, something like
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;
See http://guzzle.readthedocs.org/en/latest/quickstart.html#post-requests, http://curl.haxx.se/docs/manpage.html#-F
见http://guzzle.readthedocs.org/en/latest/quickstart.html#post-requests,http://curl.haxx.se/docs/manpage.html#-F
Edit:
编辑:
Just add ['cookies' => true]
to requests in order to use the auth cookie associated with this GuzzleHttp\Client()
. http://guzzle.readthedocs.org/en/latest/clients.html#cookies
只需添加['cookies' => true]
到请求中即可使用与此关联的 auth cookie GuzzleHttp\Client()
。http://guzzle.readthedocs.org/en/latest/clients.html#cookies
$response2 = $client->get('http://website.com/otherpage/', ['cookies' => true]);
回答by Samsquanch
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.
我在获得@JeremiahWinsley 的回答以处理较新版本的 Guzzle 时遇到了麻烦,所以我已经更新了他们的代码以从 Guzzle 5.x 开始工作。
Three major changes are required
需要三大改变
- Using
form_params
instead ofbody
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
- 使用
form_params
代替body
来防止错误“不推荐将“body”请求选项作为数组传入以发送 POST 请求。” - 更改 cookie 以使用
CookieJar
对象 - 使用
->getBody()->getContents()
得到请求体
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:
并且要在以后的请求中继续使用 cookie,请将 传递cookieJar
给请求:
$response2 = $client->get('http://website.com/otherpage/', ['cookies' => $cookieJar]);
回答by Rasel Ahmed
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.
我在获得 @JeremiahWinsley 和 @Samsquanch 的答案时遇到了麻烦,以便在较新版本的 Guzzle 上工作。所以我更新了代码以从 Guzzle 6.x 开始工作。
Guzzle 6.x. documents: http://docs.guzzlephp.org/en/stable/index.html
狂饮6.x。文件: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();
}