如何缩短这个PHP函数?

时间:2016-07-23 03:00:19

标签: php function

我多次curl_multi,我正试图找出一种缩短这段代码的方法。我正在接收数据并将其转换为JSON,然后转换为对象。

    function ($id) {

//declaring URL
$url = 'my.dev/file/.$id';

$ch_1 = curl_init($url.'6812');
$ch_2 = curl_init($url.'1359');
$ch_3 = curl_init($url.'207');
$ch_4 = curl_init($url.'1513');
$ch_5 = curl_init($url.'2361');
$ch_6 = curl_init($url.'7936');
$ch_7 = curl_init($url.'1391');
$ch_8 = curl_init($url.'1619');
$ch_9 = curl_init($url.'1617');
$ch_10 = curl_init($url.'5300');
$ch_11 = curl_init($url.'440');
$ch_12 = curl_init($url.'891');
$ch_13 = curl_init($url.'563');
$ch_14 = curl_init($url.'565');
$ch_15 = curl_init($url.'560');
$ch_16 = curl_init($url.'9143');
$ch_17 = curl_init($url.'9144');
$ch_18 = curl_init($url.'562');
$ch_19 = curl_init($url.'2434');
$ch_20 = curl_init($url.'985');
$ch_21 = curl_init($url.'1399');
$ch_22 = curl_init($url.'1373');
$ch_23 = curl_init($url.'1347');
$ch_24 = curl_init($url.'1201');
$ch_25 = curl_init($url.'1163');
$ch_26 = curl_init($url.'4087');
$ch_27 = curl_init($url.'4585');
$ch_28 = curl_init($url.'6809');
$ch_29 = curl_init($url.'566');
$ch_30 = curl_init($url.'9465');
$ch_31 = curl_init($url.'11286');


curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_4, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_5, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_6, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_7, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_8, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_9, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_10, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_11, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_12, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_13, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_14, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_15, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_16, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_17, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_18, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_19, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_20, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_21, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_22, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_23, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_24, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_25, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_26, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_27, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_28, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_29, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_30, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_31, CURLOPT_RETURNTRANSFER, true);

// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);
curl_multi_add_handle($mh, $ch_3);
curl_multi_add_handle($mh, $ch_4);
curl_multi_add_handle($mh, $ch_5);
curl_multi_add_handle($mh, $ch_6);
curl_multi_add_handle($mh, $ch_7);
curl_multi_add_handle($mh, $ch_8);
curl_multi_add_handle($mh, $ch_9);
curl_multi_add_handle($mh, $ch_10);
curl_multi_add_handle($mh, $ch_11);
curl_multi_add_handle($mh, $ch_12);
curl_multi_add_handle($mh, $ch_13);
curl_multi_add_handle($mh, $ch_14);
curl_multi_add_handle($mh, $ch_15);
curl_multi_add_handle($mh, $ch_16);
curl_multi_add_handle($mh, $ch_17);
curl_multi_add_handle($mh, $ch_18);
curl_multi_add_handle($mh, $ch_19);
curl_multi_add_handle($mh, $ch_20);
curl_multi_add_handle($mh, $ch_21);
curl_multi_add_handle($mh, $ch_22);
curl_multi_add_handle($mh, $ch_23);
curl_multi_add_handle($mh, $ch_24);
curl_multi_add_handle($mh, $ch_25);
curl_multi_add_handle($mh, $ch_26);
curl_multi_add_handle($mh, $ch_27);
curl_multi_add_handle($mh, $ch_28);
curl_multi_add_handle($mh, $ch_29);
curl_multi_add_handle($mh, $ch_30);
curl_multi_add_handle($mh, $ch_31);

// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running);

// all of our requests are done, we can now access the results
$r_1 = curl_multi_getcontent($ch_1);
$r_2 = curl_multi_getcontent($ch_2);
$r_3 = curl_multi_getcontent($ch_3);
$r_4 = curl_multi_getcontent($ch_4);
$r_5 = curl_multi_getcontent($ch_5);
$r_6 = curl_multi_getcontent($ch_6);
$r_7 = curl_multi_getcontent($ch_7);
$r_8 = curl_multi_getcontent($ch_8);
$r_9 = curl_multi_getcontent($ch_9);
$r_10 = curl_multi_getcontent($ch_10);
$r_11 = curl_multi_getcontent($ch_11);
$r_12 = curl_multi_getcontent($ch_12);
$r_13 = curl_multi_getcontent($ch_13);
$r_14 = curl_multi_getcontent($ch_14);
$r_15 = curl_multi_getcontent($ch_15);
$r_16 = curl_multi_getcontent($ch_16);
$r_17 = curl_multi_getcontent($ch_17);
$r_18 = curl_multi_getcontent($ch_18);
$r_19 = curl_multi_getcontent($ch_19);
$r_20 = curl_multi_getcontent($ch_20);
$r_21 = curl_multi_getcontent($ch_21);
$r_22 = curl_multi_getcontent($ch_22);
$r_23 = curl_multi_getcontent($ch_23);
$r_24 = curl_multi_getcontent($ch_24);
$r_25 = curl_multi_getcontent($ch_25);
$r_26 = curl_multi_getcontent($ch_26);
$r_27 = curl_multi_getcontent($ch_27);
$r_28 = curl_multi_getcontent($ch_28);
$r_29 = curl_multi_getcontent($ch_29);
$r_30 = curl_multi_getcontent($ch_30);
$r_31 = curl_multi_getcontent($ch_31);

//retornamos lista completa de items
return collect([json_decode($r_1), json_decode($r_2), json_decode($r_3),json_decode($r_4),json_decode($r_5),json_decode($r_6),json_decode($r_7),json_decode($r_8),json_decode($r_9),json_decode($r_10),json_decode($r_11),json_decode($r_12),json_decode($r_13),json_decode($r_14),json_decode($r_15),json_decode($r_16),json_decode($r_17),json_decode($r_18),json_decode($r_19),json_decode($r_20),json_decode($r_21),json_decode($r_22),json_decode($r_23),json_decode($r_24),json_decode($r_25),json_decode($r_26),json_decode($r_27),json_decode($r_28),json_decode($r_29),json_decode($r_30),json_decode($r_31)]); // same output as first example}

1 个答案:

答案 0 :(得分:1)

缩短这一点的关键是理解动态命名的变量,即变量变量See the docs

$url = "my.dev/file/.$id";
$nums = [6812,1359,207,1513...]; //numbers to append to the end of URLs
$count = count($nums); // number of curl operations

$mh = curl_multi_init();
for($i=1; $i<=$count; $i++){

    $ch = "ch_$i";

    // when $i is 1, 2... $$ch is the same as $ch_1, $ch_2... 
    $$ch = curl_init($url.$nums[$i-1]);
    curl_setopt($$ch, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($mh, $$ch);
}

// execute all queries
$running = null;
do {curl_multi_exec($mh, $running);} while ($running);

//access results
$results = [];
for($i=1; $i<=$count; $i++){
    $ch = "ch_$i";
    $r = "r_$i";

    //when $i===1, $$r === $r_1 and $$ch === $ch_1
    $$r = curl_multi_getcontent($$ch);
    $results[] = json_decode($$r);
}

return collect($results);

顺便说一下,在你的代码中,声明URL的行应该使用双引号。