Php 如何在Guzzle中链接两个http请求

Php 如何在Guzzle中链接两个http请求,php,guzzle,guzzle6,guzzlehttp,Php,Guzzle,Guzzle6,Guzzlehttp,我有两个http请求 通过mailchimp api创建新列表(将创建列表id) 将新成员添加到新创建的列表中 我对将它们链接在一起的语法有点困惑。 完整代码如下。这样做对吗 <?php // auto load require 'vendor/autoload.php'; use GuzzleHttp\Psr7\Request; // opt $option = array( 'base_uri' => "https://us12.api.mailchimp.com/3.

我有两个http请求

  • 通过mailchimp api创建新列表(将创建列表id)

  • 将新成员添加到新创建的列表中

  • 我对将它们链接在一起的语法有点困惑。 完整代码如下。这样做对吗

    <?php
    
    // auto load
    require 'vendor/autoload.php';
    
    use GuzzleHttp\Psr7\Request;
    
    // opt
    $option = array(
      'base_uri' => "https://us12.api.mailchimp.com/3.0/",
      'auth' => ['apikey', '292bae37c631ac3ba03ed0640b44e6c3'],
    );
    
    // client
    $client = new \GuzzleHttp\Client($option);
    
    // data for a new list
    $data_list = array(
      "name" => "test_mailchimp",
      "contact" => array(
        "company" => "MailChimp",
        "address1" => "675 Ponce De Leon Ave NE",
        "address2" => "Suite 5000",
        "city" => "Atlanta",
        "state" => "GA",
        "zip" => "30308",
        "country" => "US",
        "phone" => "12345678",
      ),
      "permission_reminder" => "You're receiving this email because you signed up for updates.",
      "use_archive_bar" => true,
      "campaign_defaults" => array(
        "from_name" => "test",
        "from_email" => "test@test.com",
        "subject" => "test_subject",
        "language" => "en",
      ),
        "notify_on_subscribe" => "",
        "notify_on_unsubscribe" => "",
        "email_type_option" => true,
        "visibility" => "pub",
    );
    
    
    // member data
    $data_member = array(
      'email_address' => 'member@member.com',
      "status" => "subscribed"
    );
    
    
    
    // common
    $headers = array(
      'User-Agent' => 'testing/1.0',
      'Accept'     => 'application/json'
    );
    
    
    
    // ------------- create a list -------------------
    // $data should match up the field, no json =>
    $url_display_list = 'lists';
    $req_create_list = new Request('POST', $url_display_list, $headers, json_encode($data_list));
    
    // promise
    $promise_create_list = $client
      ->sendAsync($req_create_list)
      ->then(function ($res) use ($headers, $data_member, $client) {
        $obj = json_decode($res->getBody());
        $list_id = $obj->id;
    
        // --------- add a member to list ---------
        $url_create_member = 'lists/'. $list_id. '/members';
        $req_create_member = new Request('POST', $url_create_member, $headers, json_encode($data_member));
    
        $promise_create_member = $client
          ->sendAsync($req_create_member)
          ->then(function ($res) use ($list_id) {
            $obj = json_decode($res->getBody());
            $member_id = $obj->id;
    
            echo "\n--- list id ----\n";
            echo "\n". $list_id. "\n";
    
            echo "\n--- member id ----\n";
            echo "\n". $member_id. "\n";
    
            // ------------ update a member ---------
    
    
          });
    
      });
    
    
    $promise_create_list->wait();
    $promise_create_member->wait();
    

    要创建一系列操作,只需从
    ->then()
    回调返回一个新承诺即可

    $finalPromise = $client->sendAsync('POST', $url1)->then(function (Response $response1) use ($client) {
        // Do something with the first response and prepare the second query.
    
        $secondPromise = $client->sendAsync('POST', $url2)->then(function (Response $response2) {
            // Decode JSON and/or do other stuff with the final results.
    
            return json_decode($response2->getBody()->getContents(), true);
        });
    
        return $secondPromise;
    });
    
    // The decoded JSON from the second query here.
    $response2 = $finalPromise->wait();
    
    或者您可以使用协同程序样式(我认为它不太为人所知,但更具可读性):


    要创建一系列操作,只需从
    ->then()
    回调返回一个新的承诺

    $finalPromise = $client->sendAsync('POST', $url1)->then(function (Response $response1) use ($client) {
        // Do something with the first response and prepare the second query.
    
        $secondPromise = $client->sendAsync('POST', $url2)->then(function (Response $response2) {
            // Decode JSON and/or do other stuff with the final results.
    
            return json_decode($response2->getBody()->getContents(), true);
        });
    
        return $secondPromise;
    });
    
    // The decoded JSON from the second query here.
    $response2 = $finalPromise->wait();
    
    或者您可以使用协同程序样式(我认为它不太为人所知,但更具可读性):


    我使用的是PHP7.0.15CLI,似乎没有协同程序。我需要安装什么吗?不,协同程序是Guzzle库的一部分(它是一个常用函数)。只需将其用作
    \GuzzleHttp\Promise\coroutine()
    或导入(
    使用函数GuzzleHttp\Promise\coroutine
    )。我使用的是PHP7.0.15 cli,似乎没有coroutine。我需要安装什么吗?不,协同程序是Guzzle库的一部分(它是一个常用函数)。只需将其用作
    \GuzzleHttp\Promise\coroutine()
    或导入它(
    使用函数GuzzleHttp\Promise\coroutine
    )。