C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

curl 同时发送多个请求

 

// 创建一对cURL资源
$ch1 = curl_init();
$ch2 = curl_init();

// 设置URL和相应的选项
curl_setopt($ch1, CURLOPT_URL, "http://test.cm/a.php/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt($ch2, CURLOPT_URL, "http://testd.cm/b.php");

curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt ( $ch2, CURLOPT_RETURNTRANSFER, 1 );

// 创建批处理cURL句柄
$mh = curl_multi_init();

// 增加2个句柄
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$running=null;
// 执行批处理句柄
do {
    //处理所有的请求,知道全部执行完毕
    curl_multi_exec($mh,$running);
} while($running > 0);

//根据句柄获取每个请求对应的返回的内容
$a = curl_multi_getcontent($ch1);
ee($a);
ee(curl_error($ch1)); //单个请求出错,不会影响到其他请求

$b = curl_multi_getcontent($ch2);
ee($b);

// 关闭全部句柄
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

 

curl_multi_info_read, 边读边取

// 创建一对cURL资源
$ch1 = curl_init();
$ch2 = curl_init();

// 设置URL和相应的选项
curl_setopt($ch1, CURLOPT_URL, "http://test.cm/a.php/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt($ch2, CURLOPT_URL, "http://test.cm/b.php");

curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt ( $ch2, CURLOPT_RETURNTRANSFER, 1 );

// 创建批处理cURL句柄
$mh = curl_multi_init();

// 增加2个句柄
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$running=null;
$msgs_in_queue = null;
// 执行批处理句柄
do {
    //处理所有的请求,知道全部执行完毕 , 循环执行
    $status = curl_multi_exec($mh,$running);
     $info = curl_multi_info_read($mh, $msgs_in_queue);
    if (false !== $info)
     {
          eee($msgs_in_queue);
        eee($info); //如果不为空则说明有返回结果
        eee(curl_multi_getcontent($info['handle']));
    }
    
} while($status === CURLM_CALL_MULTI_PERFORM  || $running > 0);

// 关闭全部句柄
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

 

curl_multi_select,阻塞直到cURL批处理连接中有活动连接(todo ?)

// 创建一对cURL资源
$ch1 = curl_init();
$ch2 = curl_init();

// 设置URL和相应的选项
curl_setopt($ch1, CURLOPT_URL, "http://test.cm/a.php/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt($ch2, CURLOPT_URL, "http://test.cm/b.php");

curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt ( $ch2, CURLOPT_RETURNTRANSFER, 1 );

// 创建批处理cURL句柄
$mh = curl_multi_init();

// 增加2个句柄
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

// 执行批处理句柄
$running=null;
 do {
     curl_multi_exec($mh,$running);
     $ready=curl_multi_select($mh); // this will pause the loop
     if($ready>0){
         while($info=curl_multi_info_read($mh)){
             $status=curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
             if($status==200){
                 $successUrl=curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
                 echo($successUrl);
                 print_r(curl_multi_getcontent($info['handle']));
             }
         }
     }
 } while ($running>0 && $ready!=-1);
 
// 关闭全部句柄
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

 

posted on 2015-04-12 12:36  思齐_  阅读(9041)  评论(0编辑  收藏  举报