2011-05-15 167 views
0

試圖做出捲曲處理程序,並定義它們捲曲5名兒童的,但無法找到最好的way..my代碼到目前爲止PHP通過陣列

$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 
} 

,後來定義multible捲髮:

$mh = curl_multi_init(); 

curl_multi_add_handle($mh,$ch1); 
curl_multi_add_handle($mh,$ch2); 
curl_multi_add_handle($mh,$ch3); 
curl_multi_add_handle($mh,$ch4); 
curl_multi_add_handle($mh,$ch5); 

做這個工作還是..這種最佳方式?似乎有點顛簸

+0

你爲什麼要在定義它們的循環中執行句柄?多個手柄不是並行執行的嗎? – 2011-05-15 19:07:16

+0

@Tim Yates:他們不是在做平行手柄,他們是一個接一個的登錄,當他們都登錄後,他們開始做並行工作 – Jaanus 2011-05-15 19:27:05

+0

您是否需要登錄到不同的帳戶或每個帳戶?你能不能在一次登錄後共享cookie文件? – 2011-05-15 20:11:34

回答

1

我實際上會處理這個不同的方式。我將創建一個函數來處理單個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. 

注意,你很可能包括處理錯誤和成功,如果它足夠通用的功能,但你仍然需要處理的問題因網絡問題而無法工作的單個捲曲調用等。

+0

你可以添加一些代碼嗎?我不太清楚你的意思 – Jaanus 2011-05-15 19:05:53

+0

@Jaanus見inline – 2011-05-15 19:23:59

0

您可以嘗試類似這樣的操作。我不確定它是否會在你的情況下工作,但這是一個開始。

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)