簡體   English   中英

PHP,帶有guzzle庫的異步PUT請求

[英]PHP, async PUT request with guzzle library

我正在嘗試發出異步放置請求,但某些操作不起作用,調用不執行,沒有錯誤,諾言返回“ {}”。

protected $guzzle;
function __construct()
{
    $this->guzzle = new \GuzzleHttp\Client(['cookies' => true]);
}

本身就有一個電話:

    $request = new \GuzzleHttp\Psr7\Request('PUT', $call->callback_url,
        [
            'cache-control' => 'no-cache',
            'Authorization' => 'Basic ' . $hash,
            'content-type' => 'application/x-www-form-urlencoded',
            'paymentStatusId' => "$orderstatusID"
        ]);

    $promise = $this->guzzle->sendAsync($request)->then(
        function (\Psr\Http\Message\ResponseInterface $response) use ($call) {

            if ($response->getBody() == '1') {
                $call->shopware_response = $response->getBody();
                $call->status = 'success';
            } else {
                $call->status = 'to_renewal';
            }
            $call->save();
        }
    );

    $promise->wait();

預先感謝您的答復! 干杯!

代碼看起來不錯,您可能想向其添加更多錯誤處理程序。

如果我是您:->然后(...)->否則(函數($ error){/ *以某種方式記錄錯誤* /})

還有一些小的變化:

  • $response->getBody(); $response->getBody()->getContents(); (獲取字符串,而不是流)
  • 對於基本身份驗證,最好使用sendAsync()方法的auth選項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM