2017-09-24 91 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);