cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

multi_curl with php

From: Admin <divxtamana_at_gmail.com>
Date: Wed, 22 Jun 2011 14:44:28 +0300

Hi,

after searching from couple of days i cant find solution to my problem, so i
am requesting here if someone can help.
I have a php based script which job is to post my thread with same title and
content to various sites.

currently it works well using simple curl methods by posting on each site
one by one but i see this new multi_curl feature which i can use to post on
various sites at same time so i started to work on it and developed
something as follow.

i found and used these 2 couple of functions to add sites in handle and
execute.

function addHandle(&$curlHandle,$url,$cookiefile,$postfields)
{
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01;
Windows NT 5.0)");
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cURL, CURLOPT_HEADER, 1);

if($postfields){
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $postfields);
}
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
if($cookiefile){
curl_setopt($cURL, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($cURL, CURLOPT_COOKIEFILE, $cookiefile);
}
curl_multi_add_handle($curlHandle,$cURL);
return $cURL;
}
//execute the handle until the flag passed
// to function is greater then 0
function ExecHandle(&$curlHandle)
{
$flag=null;
do {
//fetch pages in parallel
    curl_multi_exec($curlHandle,$flag);
} while ($flag > 0);
}

with using above functions i added my sites this way, $loignquery just
contain username/passwords of each sites

foreach($sites as $i => $site){

$curl[$i] = addHandle($curlHandle,$site,$cookies[$i],$loginquery[$i]);
}
// Executing above to login on site
ExecHandle($curlHandle);

With above foreach i was able to login to my sites, now there are two more
process to complete the post so i did this

$curl = NULL;
/// opening post page on site
for ($i = 1;$i <= count($site); $i++){

 $curl[$i] = addHandle($curlHandle,$postOpenPage[$i],$cookiefile[$i],0);

  }
// Executing above to open post page
ExecHandle($curlHandle);
the above opened page on those sites to send a new post, i also needed some
$_POST parameters from $postOpenPage of each site to include in my post
request. so i sued this below

// making post data
for ($i = 1;$i <= count($sites); $i++)
{
  $text[$i] = curl_multi_getcontent ($curl[$i]);
  preg_match('%name="security" value="(.*?)"%',$text[$i],$security);
  $postData = // All variables including title and body of post
with $security[0] for each site

 // now i need to post to that sites so i called one more time
 $curl[$i] = addHandle($curlHandle,$postOpenPage[$i],$cookiefile[$i],
 $postData);
}
// post executed
ExecHandle($curlHandle);

for ($i = 1;$i <= count($sites); $i++)
{
 $text2[$i] = curl_multi_getcontent ($curl[$i]);
 echo $text2[$i];
 }
for ($i = 1;$i <= count($sites); $i++) {
  curl_multi_remove_handle($curlHandle,$curl[$i]);
  }
curl_multi_close($curlHandle);

Now problem i see i am requesting a lot of time and i cant control on how
many sites i can send this post in each request so if i have 40 sites in
$sites this request goto all of them in same time, i wants it to limit to
for example process 5 sites in one time then move to next 5, also if those
threee process can be merged in one request like login, open page for post,
post thread. ( it is same post content which i need to send to various
sites).

Thanks for any help and sorry for long description and if i did
some grammar mistakes.
Best Regards

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2011-06-22