其他分享
首页 > 其他分享> > GuzzleHttp示例

GuzzleHttp示例

作者:互联网

$httpClient = new Client([
    'timeout' => 5
]);
$request = $httpClient->post("http://localhost:6000", [
    'body'    => json_encode(['name' => '测试']),
    'headers' => [
        'user-agent'   => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
        'Content-type' => 'application/x-www-form-urlencoded',
    ]
]);
echo $request->getBody()->getContents();
$client = new \GuzzleHttp\Client(['timeout' => 5]);

$jar = new \GuzzleHttp\Cookie\CookieJar();
$jar = \GuzzleHttp\Cookie\CookieJar::fromArray(
    [
        'some_cookie'  => 'foo',
        'other_cookie' => 'barbaz1234'
    ],
    'localhost'
);
$jar = new \GuzzleHttp\Cookie\FileCookieJar("/tmp/test.txt");

$url = 'http://localhost:6000/';
$request = $client->request('GET', $url, [
    'cookies' => $jar
]);

//遍历cookie 
$it = $jar->getIterator();
while ($it->valid()) {
    var_dump($it->current());
    $it->next();
}
echo $request->getBody()->getContents();

//通过名字取cookie
//$cookie = $jar->getCookieByName('some_cookie');
//
//$cookie->getValue();   // 'foo'
//$cookie->getDomain();  // 'example.org'
//$cookie->getExpires(); // expiration date as a Unix timestamp

标签:示例,request,jar,GuzzleHttp,cookie,Cookie,new
来源: https://www.cnblogs.com/dream-bccb/p/16522068.html