如何在循环中使用curl 100次发送请求并将响应存储在数组中?
例如:当curl第一次使用in循环并获取500条记录并将其存储在数组中时,再次与第二次循环进行相同的过程并从响应中获取500条记录并将其存储在同一数组中,没有任何问题。最后,我需要在数组中存储 50K 记录,并将用于在数据库中插入记录。
我最近工作了 2 天,但没有得到任何解决方案,所以请帮助我。
<?php
$final_data = array();
for($d=1;$d<=100;$d++)
{
$data = '{"request": {"header": {"username": "xxx","password": "xxx"},
"body": {
"shapes": [],
"size_to": "",
"size_from": "",
"color_from": "",
"color_to": "",
"clarity_from": "",
"clarity_to": "",
"cut_from": "",
"cut_to": "",
"polish_from": "",
"polish_to": "",
"symmetry_from": "",
"symmetry_to": "",
"labs": [],
"price_total_from": "",
"price_total_to": "",
"page_number": "1",
"page_size": "50",
"sort_by": "price",
"sort_direction": "ASC"
}}}';
$json = json_decode($data,true);
$json['request']['body']['page_number'] = $d;
$data = json_encode($json);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_URL, 'http://technet.rapaport.com/HTTP/JSON/RetailFeed/GetDiamonds.aspx');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$dd = json_decode($result,true);
foreach($dd['response']['body']['diamonds'] as $key)
{
array_push($final_data,$key);
}
curl_close($curl);
}
?>