在foreach循环中多次运行PHP文件

问题描述 投票:0回答:2

我有一个包含12500个元素的数组(公司ID)。我需要为数组中的所有元素运行1个PHP文件。

请让我知道最好的方法是什么。

我想在每个循环中使用Curl来执行文件:

<?php

$array = ['123','124','125','126','127',......,'12503'];
    foreach ($array as &$value) {
        $url = 'https://*****/*****/List_daily.php?accountid='.$value.'';
        $resp = call_curl($url);
        echo $resp."<br>"

        /**
            Here is where I want to execute the file
        **/
    }

    function call_curl($url){
        $curl = curl_init();
        curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_TIMEOUT => '5'
            ));
        $resp = curl_exec($curl);
        curl_close($curl);
        return $resp;
    }
?>
php curl
2个回答
0
投票

您可以尝试对阵列进行并行卷曲请求,以获得更好的性能,您可以将此作为示例使用PHP Parallel curl requests


0
投票

如果你没有向CURL发送任何特殊数据,你可以使用file_get_contents();

if(count($array)) {
    $url='https://*****/*****/List_daily.php?accountid=';
    foreach ($array as $v) {
        $resp = file_get_contents($url.$v);
        if($resp) {
            echo $resp."<br/>\n"; //or do something
        } else {
            //error getting data
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.