如何在循环中使用curl PHP 100次? [关闭]

2023-12-30

如何在循环中使用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);
}
?>

你可以使用卷曲多 http://php.net/manual/en/function.curl-multi-init.php,当有多个请求需要执行时,效率会更高。

$mh = curl_multi_init();
$handles = array();

for($i = 0 ; $i < 100 ; $i++){
    $ch = curl_init();
    $handles[] = $ch;

    curl_setopt($ch, CURLOPT_URL, 'http://technet.rapaport.com/HTTP/JSON/RetailFeed/GetDiamonds.aspx');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_multi_add_handle($mh,$ch);
}

$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running);

foreach($handles as $ch){
    $result = curl_multi_getcontent($ch);

    $dd = json_decode($result,true);

    foreach($dd['response']['body']['diamonds'] as $key){       
        array_push($final_data,$key);
    }   

    curl_multi_remove_handle($mh, $ch);
    curl_close($ch);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在循环中使用curl PHP 100次? [关闭] 的相关文章

随机推荐