PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How to put a delay between package of HTTP requests with Guzzle PHP?

 October 18, 2022     guzzle, httprequest, php, symfony     No comments   

Issue

I build an API with Symfony. During an action, the data coming from the front are website links and I use them to create and send asynchronous HTTP GET requests simultaneously (using of the Scrapestack API who scrapes these websites). But the fact is that the number of website links can be large and can be on the same domain. In order not to be blocked by a domain I would like to put a delay of 1sec between package of 10 requests sended simultaneously. Is it possible to do this with the PHP HTTP client Guzzle (https://github.com/guzzle/guzzle) ? Do I have to use Pool ? Here is the actual code :

$promises = [];
$results = [];
foreach ($data as $d){
   if(gettype($d) === 'string'){
     $d = json_decode($d, true);
   }
   $url = sprintf('%s?%s', 'http://api.scrapestack.com/scrape', $this->createScrapestackRequestData($d['link']));
   array_push($promises, $this->client->getAsync($url));
}
$responses = Utils::settle($promises)->wait();

Solution

SOLUTION :

$requests = [];
$results = [];
foreach ($data as $d){
  if(gettype($d) === 'string'){
    $d = json_decode($d, true);
  }
  array_push($requests, $this->curlClient->request('GET', $this->getUrlScrapestackApi($d['link'])));
}
foreach ($requests as $index => $response) {
  if ($index !== 0 && $index % 10 === 0) {
    sleep(1);
  }
  array_push($responses, $response->getContent());
}

Info : $this->curlClient is an instance of Symfony\Component\HttpClient\CurlHttpClient/



Answered By - briandev
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing