2016-09-12 12 views
1

私は、Guzzle LibraryのPOOLを使用してPOSTデータを送信しようとしています。しかし、データを送信しているアドレスでPOSTは完全に空です - 私はそれを取得しません。私も試してみましたGuzzleHttps - 非同期を送信する方法。 POST経由のデータ(プールを使用)

$client = new Client(); 

$requests = function() 
{ 
    foreach($this->urls as $url) 
    { 
     yield new Request('POST', $url, [ 
      'body' => '...' 
     ]); 
    } 
}; 

がform_paramsとマルチパート、再び動作しません(POSTは再びも$ _REQUEST & $ _GET空です)。

そしてもちろん(完全性)のコードのこの作品:

$pool = new Pool($client, $requests(), [ 
    'concurrency' => count($this->urls), 
    'fulfilled' => function ($response) {}, 
    'rejected' => function ($reason) {}, 
}); 

$promise = $pool->promise(); 
$promise->wait(); 

がつがつ食うには、(第2サーバー上で入力した)正しく要求を送信し、それ自体に任意のデータを持っていません。

私は間違っていますか?ありがとう!

EDIT:

私は(今周期で繰り返される)がつがつ食うと、このコードを置き換えしようとしている:

$ch = curl_init(); 
$url = 'https://example.cop'; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_USERAGENT, 'mehehe_net'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 59000); 
curl_setopt($ch, CURLOPT_POST, true); 
$dt = ["data" => json_encode($queue)]; 

curl_setopt($ch, CURLOPT_POSTFIELDS, $dt); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
$cont = curl_exec($ch); 
curl_close($ch); 

答えて

1

GREAT作業このソリューションは! :-)

ループの後にunwrap(wait)を使用するのを忘れないでください! :-)

0

unwrapの代わりにeach_limit()に発電機を使用することをお勧めします。

これにより、並行性レベル(並列クエリの量)を制御できます。これは、大量のリクエストとサーバー側での制限がある場合に便利です(通常、クライアントからの同時リクエストの数にはいくつかの制限があります)。

$generator = function() { 
    return $client->postAsync($uri, ['form_params' => ["alarms" => json_encode($alarm)]])->then(function ($response) { 
     // ... 
    }); 
} 

// Allow only 10 simultaneous requests. 
each_limit($generator(), 10)->wait(); 

詳細については、this articleをお読みください。

1

ここに私が使用した解決策があります。

$requests = function ($endpoints, $data) { 

      foreach ($endpoints as $endpoint) { 
       yield new Psr7\Request(
        'POST', 
        $endpoint, 
        ['Content-Type' => 'application/x-www-form-urlencoded'], 
        http_build_query($data, null, '&')); 
      } 
      echo "\n"; 
     }; 
関連する問題