日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php怎么取request,PHP-如何在Guzzle中获取Request对象?

發布時間:2025/3/8 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php怎么取request,PHP-如何在Guzzle中获取Request对象? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我需要使用Guzzle檢查數據庫中的很多項目.例如,項目數量為2000-5000.將其全部加載到單個數組中太多了,因此我想將其分成多個塊:SELECT * FROM items LIMIT100.當最后一個項目發送到Guzzle時,則請求下一個100個項目.在“已滿”處理程序中,我應該知道哪個項目得到了響應.我看到這里有$index,它指向當前項目的數量.但是我無法訪問$items變量可見的范圍.無論如何,如果我什至可以通過use($items)訪問它,那么在循環的第二遍中,我會得到錯誤的索引,因為$items數組中的索引將從0開始,而$index則大于100.因此,此方法將不起作用.

$client = new Client();

$iterator = function() {

while($items = getSomeItemsFromDb(100)) {

foreach($items as $item) {

echo "Start item #{$item['id']}";

yield new Request('GET', $item['url']);

}

}

};

$pool = new Pool($client, $iterator(), [

'concurrency' => 20,

'fulfilled' => function (ResponseInterface $response, $index) {

// how to get $item['id'] here?

},

'rejected' => function (RequestException $reason, $index) {

call_user_func($this->error_handler, $reason, $index);

}

]);

$promise = $pool->promise();

$promise->wait();

我想我能做些什么

$request = new Request('GET', $item['url']);

$request->item = $item;

然后在“已實現”的處理程序中只是為了從$response獲取$request-這將是理想的.但正如我所看到的那樣,沒有辦法做類似$response-> getRequest()的事情.

關于如何解決這個問題的任何建議?

解決方法:

不幸的是,在Guzzle中不可能收到請求.有關更多詳細信息,請參見響應創建.

但是,您可以返回一個不同的Promise,并使用each_limit()代替Pool(內部,pool類只是對EachPromise的包裝).這是更通用的解決方案,可與任何類型的諾言一起使用.

$client = new Client();

$iterator = function () use ($client) {

while ($items = getSomeItemsFromDb(100)) {

foreach ($items as $item) {

echo "Start item #{$item['id']}";

yield $client

->sendAsync(new Request('GET', $item['url']))

->then(function (ResponseInterface $response) use ($item) {

return [$item['id'], $response];

});

}

}

};

$promise = \GuzzleHttp\Promise\each_limit(

$iterator(),

20,

function ($result, $index) {

list($itemId, $response) = $result;

// ...

},

function (RequestException $reason, $index) {

call_user_func($this->error_handler, $reason, $index);

}

);

$promise->wait();

標簽:guzzle,php

來源: https://codeday.me/bug/20191111/2021098.html

總結

以上是生活随笔為你收集整理的php怎么取request,PHP-如何在Guzzle中获取Request对象?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。