Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中一次访问多个URL_Php_Api_Curl - Fatal编程技术网

在PHP中一次访问多个URL

在PHP中一次访问多个URL,php,api,curl,Php,Api,Curl,我正在开发一个API,它可以在一个请求中返回单一货币记录。一个请求的响应时间为0.5-1秒,15个请求的响应时间为7-15秒 据我所知,服务器每秒可以管理100个请求。 我想一次点击服务器上的15个请求,这样服务器将在1-2秒内给出响应,而不是在15秒内。在单个数组中返回所有数据以节省加载时间 检查我的代码 我正在使用循环,循环等待直到上一个curl请求未完成。我怎样才能说循环,继续,不要等待回应 $time_Start = microtime(true); $ids = array(1,2,

我正在开发一个API,它可以在一个请求中返回单一货币记录。一个请求的响应时间为0.5-1秒,15个请求的响应时间为7-15秒

据我所知,服务器每秒可以管理100个请求。 我想一次点击服务器上的15个请求,这样服务器将在1-2秒内给出响应,而不是在15秒内。在单个数组中返回所有数据以节省加载时间

检查我的代码

我正在使用循环,循环等待直到上一个curl请求未完成。我怎样才能说循环,继续,不要等待回应

$time_Start = microtime(true);

$ids = array(1,2,11,15,20,21); // 6 ids in demo, 15+ ids in real
$response = array();
foreach ($ids as $key => $id) {
    $response[$id] = get_data($id);
}

echo "Time: ". (microtime(true)-$time_Start)."sec";
// output 5 seconds on 6 request


function get_data($id){
    $fcs_api_key = "API_KEY";
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,"https://fcsapi.com/api/forex/indicators?id=".$id."&period=1d&access_key=".$fcs_api_key);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $buffer = curl_exec($ch);
    curl_close($ch);

    return $buffer;
}

您可以使用PHP multi-curl

下面我写了一个打开并行请求的代码

$time_Start = microtime(true);

$ids = array(1,2,3,4,5,6); // You forex currency ids.
$response = php_curl_multi($ids);

echo "Time: ". (microtime(true)-$time_Start)."sec";
// Time: 0.7 sec
作用

function php_curl_multi($ids){
    $parameters = "/api/forex/indicators?period=1d&access_key=API_KEY&id="; // ID will set dynamic
    $url        = "https://fcsapi.com".$parameters;

    $ch_index = array(); // store all curl init
    $response = array();

    // create both cURL resources
    foreach ($ids as $key => $id) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,  $url.$id);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ch_index[] = $ch;
    }

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

    //add the handles
    foreach ($ch_index as $key => $ch) {
        curl_multi_add_handle($mh,$ch);
    }

    //execute the multi handle
    do {
        $status = curl_multi_exec($mh, $active);
        if ($active) {
            curl_multi_select($mh);
        }
    } while ($active && $status == CURLM_OK);

    //close the handles
    foreach ($ch_index as $key => $ch) {
        curl_multi_remove_handle($mh, $ch);
    }
    curl_multi_close($mh);


    // get all response
    foreach ($ch_index as $key => $ch) {
        $response[] = curl_multi_getcontent($ch);
    }

    return $response;
}
见: