如何使用Guzzle发送请求?

时间:2018-04-11 09:23:46

标签: php request guzzle6

当我发送请求时,应该有转换到页面的授权。但它只是更新并仅显示授权中的页面" https://passport.yandex.ru/auth"。 请求标题

GET /php/guzzle/test.php HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
Cookie: _ym_uid=1523438286675328097; _ym_visorc_784657=b; _ym_isad=2

我的PHP代码

require 'vendor/autoload.php';
$base_url = 'https://passport.yandex.ru/auth?retpath=https%3A%2F%2Fzen.yandex.ru%2Fmedia%2Fzen%2Flogin';
$client =  new \GuzzleHttp\Client();
$jar = new \GuzzleHttp\Cookie\CookieJar();
$res = $client->request(
    'GET',
    [
        'auth' =>
        [
            'login', 
            'pass'
        ],
        ['cookies' => $jar],
        ['allow_redirects' => true]
    ]
);
$res->getStatusCode();
$response = $res->getBody();

echo $res->getStatusCode();
// 200
echo $res->getHeaderLine('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// '{"id": 1420053, "name": "guzzle", ...}'

如何用guzzle发送正确的授权请求?

1 个答案:

答案 0 :(得分:0)

这没有意义:

QStandardItem *findCityItem(const QString &city, const QString &country)
{
  auto cityItems = TreeModel->findItems(city,
                                        Qt::MatchRecursive | Qt::MatchWrap | Qt::MatchExactly, 0);
  for (auto cityItem : cityItems)
  {
    auto parent = item->parent();
    if (parent && (parent->data().toString() == country))
    {
      return item;
    }
  }
  return nullptr;
}

试试这个:

['cookies' => $jar],
['allow_redirects' => true]