Guzzle中的curl_multi_exec等效于什么?

时间:2020-08-20 08:17:45

标签: php guzzle php-curl

我正在尝试使用以下curl函数使用CURL一次创建多个webhooks,我认为它正在进行异步调用,这就是为什么与使用curl_exec相比它处理得如此之快的原因,现在我想知道如何获得相同的Guzzle的结果

public function registerWebhooks()
    {
        $topics = [
            'customers/create',
            'customers/update',
            'checkouts/create',
        ];

        foreach ($topics as $topic) {
            $params[] = [
                'webhook' => [
                    'topic'   => $topic,
                    'address' => SRC_PATH . 'includes1/webhook_handler.php',
                    'format'  => 'json'
                ]
            ];
        }

        $url = 'https://' . $this->shop . '/admin/api/2020-04/webhooks.json';

        $headers = array(
            'Content-Type:application/json',
            'X-Shopify-Access-Token:' . $this->token
        );

        $curly = array();
        // data to be returned
        $result = array();

        $mh = curl_multi_init();
        foreach ($params as $id => $d) {
            $json_params = json_encode($d);
            $curly[$id]  = curl_init();
            curl_setopt($curly[$id], CURLOPT_URL, $url);
            curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curly[$id], CURLOPT_POST, 1);
            curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $json_params);
            curl_setopt($curly[$id], CURLOPT_CUSTOMREQUEST, 'POST');
            curl_multi_add_handle($mh, $curly[$id]);
        }
        $running = null;
        do {
            curl_multi_exec($mh, $running);
        } while ($running > 0);

        foreach ($curly as $id => $c) {
            $result[$id] = json_decode(curl_multi_getcontent($c));
            curl_multi_remove_handle($mh, $c);
        }
        curl_multi_close($mh);
        return $result;
    }

1 个答案:

答案 0 :(得分:0)

您可以使用createRequest,然后再使用sendAll(),它将使用curl_multi_exec