2
I made a script to post ads automatically on a classified site for a particular user. I store the ads ID locally in the database, then with CURL, I log in, and then loop on each ad ID to activate it on the corresponding date.
But this is taking too long, getting to give timeout, as I use a CURL for each ad ID.
I wonder if there is a way to speed up the process. Searching I found the Persistent/keepalive and the 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();
This is case to run the PHP script via console/shell, there is no timeout problem.
– Bacco
By completing what Bacco said you can loop the already created variable. Instead of starting a new Curl init. This reduces SSL Handshake time if using.
– Inkeliz
Which session you want to keep, the session connection or cookie?
– Guilherme Nascimento
Cookie session, use cookiejar to save and loop with this same active session, to avoid logging in several times.
– Fabio Weydson
@Fabioweydson but if already using the cookiejar, then the first part works, only need to solve the performance? Isn’t it something in your code? It has an example similar to what you did?
– Guilherme Nascimento