Keep active session for multiple requests using CURL is very slow

2

I made a script to automatically post ads to a classified site for a particular user. I store the ads ID locally in the database, then with the CURL, I login, and then loop through each ad ID to activate it on the corresponding date.

But this is taking too long, even timeout, as I use a CURL for each ad ID.

I wonder if there is a way to speed up the process. Searching Persistent / keepalive and curl_multi_init() .

But I'm not sure how to use them.

Example of my code:

ob_start(); 
include_once("../includes/db_connect.php");
$sql = mysql_query("SELECT * FROM mytable WHERE (data_publicacao = (now() + INTERVAL 1 MINUTE) OR data_publicacao 0){

  while($annuncio = mysql_fetch_object($sql)){
    $annunci[] = trim($annuncio->url);
  }
  $url="http://sitedeanuncios.com/?page=my_profile";

  $cookie = dirname(__FILE__).'/cookies/cookies.txt';

  $username=""; 
  $password=""; 
  $postdata = "email=".$username."&password=".$password;

  $headers[] = "Connection: keep-alive";
  $headers[] = "Keep-Alive: 300";

  $ch = curl_init();
  ///////LOGIN UNICO 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
  curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  curl_setopt($ch, CURLOPT_REFERER, "http://sitedeanuncios.com");
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
  curl_setopt($ch, CURLOPT_REFERER, $url); 
  curl_setopt($ch, CURLOPT_ENCODING,  '');
  curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
  curl_setopt ($ch, CURLOPT_POST, 1); 
  $result = curl_exec ($ch);  



  foreach ($annunci as $key => $id_anuncio) {

  //////////PAGAMENTO
  $url = 'http://sitedeanuncios.com/pay/metodopay=credit&idp='.$id_anuncio;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); 
   curl_setopt($ch, CURLOPT_REFERER, "http://sitedeanuncios.com");
  curl_setopt($ch, CURLOPT_POST, 0);
  curl_setopt($ch, CURLOPT_ENCODING,  '');
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
  curl_exec ($ch);
  // //////////PUBLICACAO
  $url = 'http://sitedeanuncios.com/publish&idp='.$id_anuncio;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); 
  curl_setopt($ch, CURLOPT_POST, 0);
  curl_setopt($ch, CURLOPT_ENCODING,  '');
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
  curl_exec($ch); 
  curl_close($ch);

  ob_flush();

  }
}

ob_end_flush();
    
asked by anonymous 01.07.2016 / 18:24

0 answers