出于某种原因,我不知道为什么我的脚本会停止。它运行大约 20分钟,然后停止而不检索所有记录。我确定有什么东西导致它停止,但我不知道是什么。如果它是内存不足,那么我想会抛出错误信息,但事实并非如此。 此项目将于下周发布。我们通过 cron job 运行此脚本。请帮忙!
<?php
set_time_limit(0);
ini_set('max_execution_time', 0);
ini_set('memory_limit','4G');
$Num = $_GET['Num'];
$Token = $_GET['Token'];
if(strlen($Num) > 0){
$j = $Num;
$j = 1;
}
else{
$j = 0;
}
$e = 0;
$k = 0;
$g = 1;
$x=0;
$json = '';
$EmailIDs = array();
$leads = new bList();
$leads->listId = 2867;
$AllLeads = array();
$AllLeads = $leads->fields;
if(strlen($Token) > 0){
$NextPage = $Token;
$leads->nextPageToken = $NextPage;
}
RUN();
function RUN(){
global $leads;
global $k;
global $json;
global $Next;
if($k == 0){
AreUnsubscribed($leads->getData());
$k = 1;
$Next = $json[nextPageToken];
$NextPage = $Next;
}
else{
AreUnsubscribed($leads->getData());
$Next = $json[nextPageToken];
if($Next != null){
$NextPage = $Next;
unset($Next);
}
else{
$msg = 'Status = no NextPage' ;
$msg = wordwrap($msg,70);
mail("email@domain.com",'Status = no NextPage - Done',$msg);
}
}
unset($leads,$json,$Next);
}
class bList {
private $host = "Some URL";//CHANGE ME
private $clientId = "###############";//CHANGE ME
private $clientSecret = "###############";//CHANGE ME
public $listId;//id of list to retrieve leads from
public $fields = array("Time","SignUp","createdDate", etc....)
public $batchSize; //max 300 default 300
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;
}
}
function AreUnsubscribed($G){
global $k;
global $AllLeads;
global $EmailIDs;
global $j;
global $e;
global $json;
global $Next;
$json = json_decode($G, true);
if($j == 0){
$EmailIDs[0] = $AllLeads;
$j=1;
}
if($e == 0){
for($n = 0; $n<=300; $n++){
$SORTING = $json['result'][$n];
if($SORTING == null){
break;
}
else{
array_push($EmailIDs, jj($SORTING));
}
unset($SORTING);
}
$e = 1;
$k =1;
RUN();
}
if($k == 1){
Part2($json);
}
unset($EmailIDs, $json, $AllLeads);
}
function Part2($json){
global $EmailIDs;
global $g;
for($n = 0; $n<=300; $n++, $g++){ // 300 records
$SORTING = $json['result'][$n];
if($SORTING == null){
break;
} else{
$EmailIDs[$g] = array_merge((array)$EmailIDs[$g], jj($SORTING));// position
}
unset($SORTING);
}
GG($EmailIDs);
unset($EmailIDs, $json);
}
function jj($kk){
$NewArray = array();
global $AllLeads;
global $e;
if($e == 0){
for($b = 0; $b<270; $b++){
if($kk[$AllLeads[$b]] == null){
array_push($NewArray, $kk[$AllLeads[$b]]);
}
else{
array_push($NewArray, '"'.$kk[$AllLeads[$b]].'"');
}
}
$kk = null;
} else{
for($b = 270; $b<540; $b++){
if($kk[$AllLeads[$b]] == null){
array_push($NewArray, $kk[$AllLeads[$b]]);
}else{
array_push($NewArray, '"'.$kk[$AllLeads[$b]].'"');
}
}
$kk = null;
}
unset($EmailIDs, $json, $kk);
return $NewArray;
unset($NewArray);
}
function GG($val){
$datetime = date('Y-m-d');
global $EmailIDs;
global $x;
global $e;
global $g;
global $Next;
global $j;
global $json;
$handle = fopen("New" . "-Export-" . $datetime . ".csv", "a");
foreach($val as $value){
fputcsv($handle, (array)$value);
}
fclose($handle);
unset($val);
//echo '<pre>';
//$vars = get_defined_vars();
//foreach($vars as $name=>$var)
//{
// echo '<strong>' . $name . '</strong>: ' . strlen(serialize($var)) . '<br/>';
//}
$EmailIDs = array();
$Next = $json[nextPageToken];
if($Next != null){
print_r('{"Number":"'. $j . '", "Token":"' . $Next .'"}');
unset($EmailIDs, $json);
unset($GLOBALS['leads']);
exit;
}else{
print_r('Finished');
}
}
?>