在循环时执行CURL

时间:2016-11-24 17:19:37

标签: php json curl time response

这是我第一次使用CURL,我遇到了一个大问题: 我使用lopp来获取来自大型mysql数据库的数据并且需要花费大量时间来执行,我的问题是:有没有办法优化我的脚本?因此我的表包含30000行,因此执行时间减少了 这是我使用的代码:

while($res = mysqli_fetch_array($run_requete)){

++$i ;

$field= $res['field'];  
    //API URL
$url = 'http://www.google.com'; //API url

$data = array(
    "value" => $field,
    );

$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "someuser:somepass");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1000);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json','Content-Length: ' . strlen($data_string)));
$resp = curl_exec($ch);
curl_close($ch);
   $result = json_decode($resp, true);

echo '<span style="display:none;">';
    $respon = var_dump($result);
echo'</span><br>';

$count = 0;
foreach ($result as $type) {
    $count+= count($type);
}

$c = $count;
if($c == 2){

**//GET CURL RESPONSE VARS....
//==>> then put it in mysql table**

$insertion_result = "INSERT INTO `result_test`(`field1`, `field2`, `field3`, `field4`, `field5`, `field6`)VALUES(`field1`, `field2`, `field3`, `field4`, `field5`, `field6`)";
$add_res = mysqli_query($con, $insertion_result);

}else if($c == 1){
    //GET CURL RESPONSE VARS....

//==>> then INSERT in mysql table

$insertion_result = "INSERT INTO `result_test`(`field1`, `field2`, `field3`, `field4`, `field5`, `field6`)VALUES(`field1`, `field2`, `field3`, `field4`, `field5`, `field6`)";
$add_res = mysqli_query($con, $insertion_result);

}else if($c == 6){

// some instruction

}else {
    echo "error message";
}

}

此问题的目的是优化缓冲区以缩短响应时间,感谢您对我的问题的兴趣。

0 个答案:

没有答案