2017-09-24 112 views
-1

我想建立一個腳本來一次執行多個URL(比如說100)。 我使用的代碼PHP腳本來執行多個網址?我的腳本不工作

<?php 
// create a new cURL resource 
$ch = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "url1"); 




curl_setopt($ch, CURLOPT_URL, "url2"); 




curl_setopt($ch, CURLOPT_URL, "url3"); 
curl_setopt($ch, CURLOPT_URL, "url4"); 
curl_setopt($ch, CURLOPT_URL, "url15"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 




// grab URL and pass it to the browser 
curl_exec($ch); 
// close cURL resource, and free up system resources 
curl_close($ch); 
?> 

的劇本,但劇本是隻執行最後兩個網址,不是全部。一個正確的腳本應該執行多個url?

+1

這裏的例子,而不是增加一個引擎收錄鏈接,請。 –

+0

請閱讀:https://stackoverflow.com/help/asking –

回答

1

CURLOPT_URL只是設置一個字符串,設置它兩次覆蓋以前的值。 你可能尋找curl_multi,它可以同時執行多個URL,並且是一個痛苦的使用,檢查您可以粘貼代碼的http://php.net/manual/en/function.curl-multi-exec.php

<?php 
// create both cURL resources 
$ch1 = curl_init(); 
$ch2 = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch1, CURLOPT_URL, "url1"); 
curl_setopt($ch1, CURLOPT_HEADER, 0); 
curl_setopt($ch2, CURLOPT_URL, "url2"); 
curl_setopt($ch2, CURLOPT_HEADER, 0); 

//create the multiple cURL handle 
$mh = curl_multi_init(); 

//add the two handles 
curl_multi_add_handle($mh,$ch1); 
curl_multi_add_handle($mh,$ch2); 

$active = null; 
//execute the handles 
do { 
    $mrc = curl_multi_exec($mh, $active); 
} while ($mrc == CURLM_CALL_MULTI_PERFORM); 

while ($active && $mrc == CURLM_OK) { 
    if (curl_multi_select($mh) != -1) { 
     do { 
      $mrc = curl_multi_exec($mh, $active); 
     } while ($mrc == CURLM_CALL_MULTI_PERFORM); 
    } 
} 

//close the handles 
curl_multi_remove_handle($mh, $ch1); 
curl_multi_remove_handle($mh, $ch2); 
curl_multi_close($mh);