如何在100次循环中使用curl PHP? [英] How to use curl PHP in loop with 100 time?

查看:92
本文介绍了如何在100次循环中使用curl PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在发送请求中将curl循环使用100次并将响应存储在数组中?

How to use curl in loop 100 times with send request and store response in an array?

例如:当curl第一次在循环中使用时,获得500条记录并存储在一个数组中,第二次循环中再次使用相同的过程,并从响应中获得500条记录并存储在同一数组中而没有任何问题.最后,我需要在阵列中存储5万条记录,并将其用于数据库中的插入记录.

For example : when curl uses in loop first time and gets 500 records and store in an array and again the same process with the second loop and gets 500 records form response and store in the same array without any issues. Finally, I need store 50K records in the array and I will use for insert records in my database.

我最近两天都在工作,但没有得到任何解决方案,因此请帮助我.

I am working last 2 days but not getting any solution for this so please help me.

<?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);
}
?>

推荐答案

您可以使用

You could use curl_multi, it is more efficient when having several requests to perform.

$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);
}

这篇关于如何在100次循环中使用curl PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆