zoukankan      html  css  js  c++  java
  • guzzlephp使用教程

    参考文档:http://docs.guzzlephp.org/en/latest/overview.html

    1、composer安装

    {
       "require": {
          "guzzlehttp/guzzle": "~6.0"
       }
    }
    

    2、多请求并行

    use GuzzleHttp\Client;
    use GuzzleHttp\Promise;
    
    $client = new Client();
    
    // Initiate each request but do not block
    $promises = [
        'req1' => $client->getAsync('http://api.test.com/req1'),
        'req2' => $client->getAsync('http://api.test.com/req2')
    ];
    
    // Wait for the requests to complete, even if some of them fail
    $results = Promise\settle($promises)->wait();
    
    // You can access each result using the key provided to the unwrap
    // function.
    echo $results['req1']->getHeader('Content-Length');
    echo $results['req2']->getHeader('Content-Length');
    

    3、并行请求拿结果

    $requests = function() use ($comm_ids) {
        $base_url = APF::get_instance()->get_config('broker_basic_url');
        foreach ($comm_ids as $comm_id) {
            $url = $base_url . 'broker/commSignTopSigner/?is_nocheck=1&commId=' . $comm_id;
            yield new GuzzleHttp\Psr7\Request('GET', $url);
        }
    };
    
    方法一:
    $responses = [];
    $pool = new GuzzleHttp\Pool(new GuzzleHttp\Client(), $requests(), [
        'concurrency' => 5,
        'fulfilled' => function ($response, $index) use (&$responses, $comm_ids) {
            $responses[$index] = $response;
        },
        'rejected' => function ($reason, $index) use (&$responses, $comm_ids) {
            $responses[$index] = [];
        },
    ]);
    
    $pool->promise()->wait();
    
    // 输出response集合
    foreach ($responses as $response) {
        $response->getBody()->getContents()
    }
    
    
    方法二:
    $container = [];
    $stack = GuzzleHttp\HandlerStack::create();
    $stack->push(GuzzleHttp\Middleware::history($container));
    $client = new GuzzleHttp\Client(['handler' => $stack]);
    $pool = new GuzzleHttp\Pool($client, $requests(), ['concurrency' => 5]);
    $pool->promise()->wait();
    
    foreach ($container as $transaction) {
        parse_str($transaction['request']->getUri()->getQuery(), $query);
        print_r($query);
        print_r($transaction['response']->getBody()->getContents());
    }
    

    3、POST请求

    // 根据不同的Content-Type生成对应的stream
    
    // application/json
    $stream = GuzzleHttp\Psr7\stream_for('{"a":"1", "b":"2"}');
    
    // application/x-www-form-urlencoded
    $stream = GuzzleHttp\Psr7\stream_for('a=1&b=2');
    
    $header = [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ];
    
    $url = 'xxx';
    
    $request = new \GuzzleHttp\Psr7\Request('POST', $url, $header, $stream);
    
    $_POST取参数
    
    $params = http_build_query([
        'a' => 1
    ]);
    $stream = \GuzzleHttp\Psr7\stream_for($params);
    $header = ['Content-Type' => 'application/x-www-form-urlencoded'];
    $requests[] = new \GuzzleHttp\Psr7\Request('POST', $url, $header, $stream);
    
  • 相关阅读:
    P4781 【模板】拉格朗日插值
    P1306 斐波那契公约数
    P1154 奶牛分厩
    P1028 数的计算
    P1445 [Violet]樱花
    2020 Multi-University Training Contest 4
    Codeforces Round #658 (Div. 2) D
    2020牛客暑期多校训练营(第八场) K
    Codeforces Round #659 (Div. 2)
    #10106. 「一本通 3.7 例 2」单词游戏
  • 原文地址:https://www.cnblogs.com/chenguoli/p/7607172.html
Copyright © 2011-2022 走看看