PHP通过数组定义多表卷曲

PHP通过数组定义多表卷曲,php,arrays,curl,Php,Arrays,Curl,尝试为curl处理程序生成5个curl child并定义它们,但找不到最佳方法..我的代码到目前为止 $curls = array($ch1, $ch2, $ch3, $ch4, $ch5); // have a bad feelin about this $cont = array($cont1, $cont2, $cont3, $cont4, $cont5); // bad for($i = 0; $i < count($curls); $i++) { // bad $cur

尝试为curl处理程序生成5个curl child并定义它们,但找不到最佳方法..我的代码到目前为止

$curls = array($ch1, $ch2, $ch3, $ch4, $ch5); // have a bad feelin about this
$cont = array($cont1, $cont2, $cont3, $cont4, $cont5); // bad

for($i = 0; $i < count($curls); $i++) { // bad
    $curls[$i] = curl_init();

    curl_setopt($curls[$i], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curls[$i], CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curls[$i], CURLOPT_REFERER, $ref);
    curl_setopt($curls[$i], CURLOPT_USERAGENT, $useragent);

    curl_setopt($curls[$i], CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curls[$i], CURLOPT_COOKIEJAR, $cookiefile);

    curl_setopt($curls[$i], CURLOPT_URL, $url);
    curl_setopt($curls[$i], CURLOPT_POST, true);
    curl_setopt($curls[$i], CURLOPT_POSTFIELDS, $data);
    $cont[$i] = curl_exec($curls[$i]); //  bad

    curl_setopt($curls[$i], CURLOPT_URL, $url);
    curl_setopt($curls[$i], CURLOPT_POST, true);
    curl_setopt($curls[$i], CURLOPT_POSTFIELDS, $data);
    $cont[$i] = curl_exec($curls[$i]); // bad
}

这是有效的还是……这是最佳的方式?看起来有点颠簸

实际上我会用另一种方式来处理。我将创建一个函数来处理
curl
的单个实例的创建,并根据需要使用参数来调整变量设置。这使我可以创建一个实例,也可以创建一个循环来创建多个实例

问题是,很多时候,多个curl调用取决于前一个调用是否成功。如果第一个没有成功,我现在已经浪费地分配了多个curl对象。创建第一个,运行它,错误检查,创建第二个,运行它,错误检查,等等。这样你只分配你需要的

编辑:类似这样的内容

// get the result of a single curl call
function makeCurlCall($ref, $useragent, $cookiefile, $url, $data)
{
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curl, CURLOPT_REFERER, $ref);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);

    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $cont = curl_exec($curl);

    // May need to use this later
    $error_no = curl_errno($curl);

    if($error_no) {
      // so we can close before we return
      $result = "[" . $error_no . "] " . curl_error($curl);
      curl_close($curl);
      return array('status' => 'error', 'result' => $result);
    }
    else {
      curl_close($curl);
      return array('status' => 'success', 'result' => $cont);
    }
}

$curl = makeCurlCall($ref, $useragent, $cookiefile, $url, $data);
if($curl['status'] == 'error') {
  // do something for the error
}
else { 
  // do something with $curl['result']
}

// The first call worked, so make the next call, only allocating what we need
$curl = makeCurlCall($ref, $useragent, $cookiefile, $url, $data);

//etc.

请注意,如果它足够通用,您可能会包括处理错误和成功的功能,但是您仍然需要处理由于网络问题等导致单个curl调用无法工作的问题。

您可以尝试类似的方法。我不确定它是否适用于您的场景,但这只是一个开始

function makeCurl($ref, $useragent, $cookiefile, $url, $data){
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curl, CURLOPT_REFERER, $ref);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);

    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);

    curl_setopt($curl, CURLOPT_URL, $url);

    if($data){
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    return $curl;
}

// first let's login

$login = makeCurl($loginref, $useragent, $cookiefile, $loginurl, $logindata);
curl_exec($login);
curl_close($login);

// $cookiefile now has the needed cookies
// for the parallel jobs to run

$curls = array();
$mh = curl_multi_init();

$curls[0] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[1] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[2] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[3] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[4] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[5] = makeCurl($ref, $useragent, $cookiefile, $url, $data);

for($i = 0; $i < count($curls); $i++){
    curl_multi_add_handle($mh, $curls[$i]);
}

// use curl_multi_exec to run them and wait for them to finish
// (I've never done this before, so i can't tell you how to do it)
函数makeCurl($ref、$useragent、$cookiefile、$url、$data){
$curl=curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl,CURLOPT_REFERER,$ref);
curl_setopt($curl,CURLOPT_USERAGENT,$USERAGENT);
curl_setopt($curl,CURLOPT_COOKIEFILE,$COOKIEFILE);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookiefile);
curl_setopt($curl,CURLOPT_URL,$URL);
如果($数据){
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
返回$curl;
}
//首先让我们登录
$login=makeCurl($loginref、$useragent、$cookiefile、$loginurl、$loginda);
curl_exec($login);
curl_close($login);
//$cookiefile现在具有所需的cookies
//让并行作业运行
$curls=array();
$mh=curl_multi_init();
$curls[0]=makeCurl($ref、$useragent、$cookiefile、$url、$data);
$curls[1]=makeCurl($ref、$useragent、$cookiefile、$url、$data);
$curls[2]=makeCurl($ref、$useragent、$cookiefile、$url、$data);
$curls[3]=makeCurl($ref、$useragent、$cookiefile、$url、$data);
$curls[4]=makeCurl($ref、$useragent、$cookiefile、$url、$data);
$curls[5]=makeCurl($ref、$useragent、$cookiefile、$url、$data);
对于($i=0;$i
您能用它添加一些代码吗?我不太清楚你的意思,为什么要在定义句柄的循环中执行句柄?多个句柄并行处理不是有意义吗?@Tim Yates:他们还没有并行处理,而是一个接一个地登录,当他们全部登录后,他们就开始并行处理。你需要登录到不同的帐户还是每个帐户?你不能在一次登录后共享cookie文件吗?$Ryan Pendelton:与所有5个curl儿童的帐户相同,共享cookie文件?怎样?
function makeCurl($ref, $useragent, $cookiefile, $url, $data){
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curl, CURLOPT_REFERER, $ref);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);

    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);

    curl_setopt($curl, CURLOPT_URL, $url);

    if($data){
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    return $curl;
}

// first let's login

$login = makeCurl($loginref, $useragent, $cookiefile, $loginurl, $logindata);
curl_exec($login);
curl_close($login);

// $cookiefile now has the needed cookies
// for the parallel jobs to run

$curls = array();
$mh = curl_multi_init();

$curls[0] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[1] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[2] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[3] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[4] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[5] = makeCurl($ref, $useragent, $cookiefile, $url, $data);

for($i = 0; $i < count($curls); $i++){
    curl_multi_add_handle($mh, $curls[$i]);
}

// use curl_multi_exec to run them and wait for them to finish
// (I've never done this before, so i can't tell you how to do it)