php - 如何在 Guzzle 通话中通过登录屏幕

标签 php curl laravel guzzle

我必须使用 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/

相关文章:

php - HTTPS 子域不断重定向到 HTTPS 主域

javascript - 使用 AJAX 向 PHP Web 服务提交基本表单数据

php - 将随机 ID 插入数据库

javascript - PHP : Get values outside of foreach

php - Laravel 表单请求的自定义错误代码

php - PHP 中的 PATH_INFO 到底是什么?

PHP/JSON - 如何解析 stdClass 对象?

php - 单点登录网站

php - PHP的curl不加载整页内容而只加载gif

php - 如何在php中使用数据库中的数据设置“阅读更多”按钮