Php 从API获取数据时诊断瓶颈

Php 从API获取数据时诊断瓶颈,php,mysql,optimization,curl,proxy,Php,Mysql,Optimization,Curl,Proxy,我正在运行一个从API服务器获取数据的专用服务器。我的机器运行在Windows Server 2008操作系统上 我使用PHP curl函数通过http请求(并使用代理)获取数据。我为此创建的函数: function get_http($url) { $proxy_file = file_get_contents("proxylist.txt"); $proxy_file = explode(" ", $proxy_file); $how_Many_Proxies = count($prox

我正在运行一个从API服务器获取数据的专用服务器。我的机器运行在Windows Server 2008操作系统上

我使用PHP curl函数通过http请求(并使用代理)获取数据。我为此创建的函数:

function get_http($url)
{

$proxy_file = file_get_contents("proxylist.txt");
$proxy_file = explode("
", $proxy_file);

$how_Many_Proxies = count($proxy_file);

$which_Proxy = rand(0,$how_Many_Proxies);


$proxy = $proxy_file[$which_Proxy];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

return $curl_scraped_page;
}
然后,我使用以下简单代码将其保存在MySQL数据库中:我与curl并行运行20-40-60-100版本(经过一段时间后,它不会提高性能,我想知道瓶颈在哪里?)

函数检索数据($id)
{
$the_data=get_http(“http://api-service-ip-address/?id=$id”);
返回$u数据;
}
$ids\U List=文件获取内容(“List.txt”);
$ids\U List=分解(“
“,$ids_列表);
对于($a=0;$a)
  • 每次调用curl函数时,您都在读取代理文件。我建议您在函数外部使用read操作。我的意思是读取代理一次,并将其存储在数组中以重用它

  • 使用此curl选项CURLOPT_TIMEOUT为curl执行定义一个固定的时间量(例如3秒)。它将帮助您调试是否存在curl操作问题


  • 你用xdebug和webgrind分析过它吗?
    function retrieveData($id)
    {
    
    $the_data = get_http("http://api-service-ip-address/?id=$id");
    
    return $the_data;   
    
    }
    
    $ids_List = file_get_contents("the-list.txt");
    $ids_List = explode("
    ",$ids_List);
    
    for($a = 0;$a<50;$a++)
    
    {
    
    $array[$a] = get_http($ids_List[$a]);
    
    }
    
    
        for($b = 0;$b<50;$b++)
        {
    
    
        $insert_Array[] = "('$ids_List[$b]', NULL, '$array[$b]')";
    
    
        }
        $insert_Array = implode(',', $insert_Array);
    
        $sql = "INSERT INTO `the_data` (`id`, `queue_id`, `data`) VALUES $insert_Array;";
    
        mysql_query($sql);