Forgot password?
 Register now

[software engineering] Quick Start with GuzzleHttp and Carrying Cookies

[Copy link]
Post time 2022-4-18 09:42:44 | Show all posts |Read mode
In some synchronous batch business scenarios, request implementation is required, but curl is a bit slow, so guzzlehttp is introduced to solve this problem
Initiate a synchronous request and carry a cookie
function doladall($url, $arr)
{
    //sync request
    $client = new GuzzleHttp\Client();
    //set cookie

    $domain = parse_url($url)['host'];
    $values ​​= [
        'user_code' => 'xxx',
    ];
    $cookieJar = CookieJar::fromArray($values, $domain);
    $res = $client->request('POST', $url, [
        'cookies' => $cookieJar,
        'form_params' => $arr
// 'query' => $arr
    ]);
    echo $res->getBody();
}
Initiate an asynchronous request and carry a cookie
function syncpost($url, $arr)
{
    $client = new Client();

    //set cookie
    $domain = parse_url($url)['host'];
    $values ​​= [
        'user_code' => 'xxx',
    ];
    $cookieJar = CookieJar::fromArray($values, $domain);

    $request = new Request('POST', $url);
$promise = $client->sendAsync($request,['cookies' => $cookieJar,'form_params' => $arr])->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});

$promise->wait();

}
Simulate login and get cookie value
$arr = [
    'name'=>'xxx',
    'pwd'=>'xxx',
];
$client = new GuzzleHttp\Client(['base_uri' => 'http://localhost:9091/index/index/']);
$res = $client->request('POST', 'logincheck',[
    'form_params'=>$arr
]);
$head_arr = $res->getHeader('Set-Cookie');
$cookie_arr = [];
foreach ($head_arr as $val){
    $new_sub = explode('=',substr($val,0,strpos($val,';')));
    $cookie_arr[$new_sub[0]] = $new_sub[1];
}
var_dump($cookie_arr);exit;





the last one:压缩备份数据库与网站数据(mysql不停止的方法)简易实用BAT
the next one:Emlog template article password page style

Use magic Report

Mobile version|Small black house|Chinese Version|I love source code

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved

About this site - Copyright statement - Ln Studio! - Advertising contact

The resources on this site come from the Internet and are only for user testing. The relevant copyright belongs to the original author.

快速回复 To Top 返回列表