php从数据库

时间:2017-10-30 16:37:26

标签: php arrays csv out-of-memory php-curl

我有一个项目,我需要从数据库中提取多达50,000条记录,并使用该数据创建一个csv文件。我的问题是服务器一次只能返回300条记录。如何有效地运行此脚本apx 166次,同时将服务器中的每个响应存储在一个数组中,而不会耗尽内存?

一旦完成了服务器的最后一次呼叫请求,我将运行该功能(未显示)以从阵列中创建CSV。

class RetrieveData {

public $fields = array("Name1", "Name2", "Name3", "Name4");// List of names to pull from database

public $batchSize; //max 300 records returned at a time 
public $NextPage;
public $nextPageToken; //token returned from previous call for paging


public function getData(){

    $url = $this->host . "/rest/v1/list/" . $this->listId . "/leads.json?access_token=" . $this->getToken();
    if (isset($this->fields)){
        $url = $url . "&fields=" . $this::csvString($this->fields);
    }
    if (isset($this->batchSize)){
        $url = $url . "&batchSize=" . $this->batchSize;
    }
    if (isset($this->nextPageToken)){
        $url = $url . "&nextPageToken=" . $this->nextPageToken;
    }

    $ch = curl_init($url);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
    $response = curl_exec($ch);
    return $response;
}

private function getToken(){
    $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
    $response = json_decode(curl_exec($ch));
    curl_close($ch);
    $token = $response->access_token;
    return $token;
}

    private static function csvString($fields){

    $csvString = "";

    $rr= implode(",",$fields);

    $FF = str_replace(' ', '', $rr);

    $csvString = $FF;

    $csvString = rtrim($csvString,',');
    }

    return $csvString;
}

}

0 个答案:

没有答案