我必须使用 cURL 将信息发送到外部网站。我在我的 Laravel 应用程序上设置了 Guzzle。我已经设置了基础知识,但根据网站的文档,用户名和密码需要执行一项操作。如何传递“操作”以及登录和获取访问权限所需的凭据?
网站声明:
curl [-k] –dump-header <header_file> -F “action=login” -F “username=<username>” -F “password=<password>” https://<website_URL>
我的 Controller :
$client = new \GuzzleHttp\Client();
$response = $client->get('http://website.com/page/login/', array(
'auth' => array('username', 'password')
));
$xml = $response;
echo $xml;
该网站将在 echo
上加载, 但它只会拉出登录屏幕。我需要这些凭据来绕过登录屏幕(成功登录后)以获取 cURL 所需的信息部分。
最佳答案
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]);
关于php - 如何在 Guzzle 通话中通过登录屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089960/