如何在 Guzzle 通话中跳过登录屏幕 [英] How to get past login screen on Guzzle call
问题描述
我必须使用 cURL 将信息发送到外部网站.我在 Laravel 应用程序上设置了 Guzzle.我已经设置了基础知识,但是根据网站的文档,需要对用户名和密码进行操作.如何传递操作"以及登录和获取访问权限所需的凭据?
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 [-k] –dump-header
我的控制器:
$client = new GuzzleHttpClient();
$response = $client->get('http://website.com/page/login/', array(
'auth' => array('username', 'password')
));
$xml = $response;
echo $xml;
网站将在 echo
上加载,但只会拉出登录屏幕.我需要这些凭据来绕过登录屏幕(登录成功)以获取 cURL 所需的部分信息.
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.
推荐答案
curl -F
提交 POST 请求而不是 GET 请求.所以你需要相应地修改你的代码,比如
curl -F
submits a POST request instead of a GET request. So you'll need to modify your code accordingly, something like
$client = new GuzzleHttpClient();
$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#发布请求,http://curl.haxx.se/docs/manpage.html#-F
只需添加 ['cookies' =>true]
请求以使用与此 GuzzleHttpClient()
关联的身份验证 cookie.http://guzzle.readthedocs.org/en/latest/clients.html#cookies
Just add ['cookies' => true]
to requests in order to use the auth cookie associated with this GuzzleHttpClient()
. http://guzzle.readthedocs.org/en/latest/clients.html#cookies
$response2 = $client->get('http://website.com/otherpage/', ['cookies' => true]);
这篇关于如何在 Guzzle 通话中跳过登录屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!