Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP批量上传数组数据_Php_Timeout - Fatal编程技术网

PHP批量上传数组数据

PHP批量上传数组数据,php,timeout,Php,Timeout,我使用的客户端API(主API)没有批量功能 我从2个不同的API(客户端API)中获取数据,并将其合并到一个格式正确的JSON文件中。签入在线JSON验证程序 JSON文件包含1100条合并客户数据记录。每次记录一条记录,我构建了一个函数,可以成功地将数据提交给主API 现在,我已经构建了一个PHP脚本,该脚本在JSON文件中循环,获取行数据(每个客户端记录)并将其成功提交给主API。大约90行之后,PHP脚本超时 我在页面上设置了以下代码 @ini_set('zlib.output_comp

我使用的客户端API(主API)没有批量功能

我从2个不同的API(客户端API)中获取数据,并将其合并到一个格式正确的JSON文件中。签入在线JSON验证程序

JSON文件包含1100条合并客户数据记录。每次记录一条记录,我构建了一个函数,可以成功地将数据提交给主API

现在,我已经构建了一个PHP脚本,该脚本在JSON文件中循环,获取行数据(每个客户端记录)并将其成功提交给主API。大约90行之后,PHP脚本超时

我在页面上设置了以下代码

@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
set_time_limit(600);
@ob_end_clean();
我会缓冲每次更新,以返回从主API返回的JSON状态代码

我应该怎么做才能让PHP在大约100条记录之后不超时,并不断更新页面上的缓冲响应

提前谢谢


杰森

我过去曾采取过几种不同的方法来解决这类问题。唯一的快速修复方法是,如果您可以更改服务器上的php.ini设置,以增加足够的超时时间,从而允许批处理完成。这不是一个很好的解决方案,但它是一个解决方案

下一个选项(按工作的升序)是在浏览器和服务器之间设置一个循环,在该循环中,浏览器发出请求,服务器发送部分记录,然后返回浏览器,并使用光标指示流程停止的位置,浏览器向服务器发出另一个请求,将光标作为参数发送回,这将一直持续到批处理完成。这很好,因为您可以向用户显示进度条

最后,您可以在服务器上运行一个代理,等待提交批处理作业,并在HTTP请求生命周期之外完全运行它们。因此,浏览器会发出启动批处理作业的请求,从而在数据库中生成某种记录,以跟踪作业的状态。代理在作业工作时拾取作业并将其设置为挂起状态,然后在作业完成时设置完成状态。您可以设置允许您定期从浏览器轮询服务器的内容,以便在进程完成时向用户发出警报。当然,您可以让代理在批处理完成时向用户发送电子邮件报告。这是最可靠的选项,具有最小的中断处理的风险,并且它可以在不做任何努力的情况下留下审计跟踪。但是设置起来显然更复杂。

谢谢Rob

你的回答让我找到了正确的方向

我在前端使用了你的后端想法。我只是一次循环浏览20条记录,然后通过javascript刷新页面,从21点到40点等等。为了好玩,我还加入了一个进度条

谢谢你帮我了解这个想法。这不是正确的方法,但是我的Python和我的PHP一样糟糕

<?php
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
set_time_limit(600);
@ob_end_clean();

require("header.php");
require_once('nav.php');

function sync_systems($sfData){

    $dataPost = $sfData;
    $postID = $dataPost['ID'];
    

    $epcall = update_profile($dataPost);
   
    $epResult = json_decode($epcall, true);
    if($epResult['status'] != 404){
        
        
        $sfStatus = updateSFOpportunity($epResult['client_id'], $epResult['ep_id'] );
        
        if($sfStatus == 1){
        $datamsg = " Success! The sync was a success in both Salesforce and other system. OtherSystem Record " . $epResult['ep_id'] . " was created or updated.<br/>";
        } else {
            $datamsg = " Success! The sync was a success in other system, but failed in Salesforce<br/>";
        }
            echo json_encode(['code'=>200, 'msg'=>$datamsg]);
    } else {
        $datamsg = " Failure! The sync did not work.<br/>";
            echo json_encode(['code'=>404, 'msg'=>$datamsg]);
    } // end epResult




}

function sync_ep($sfData){

    $dataPost = $sfData;
    $postID = $dataPost['ID'];
    

    $epcall = update_profile($dataPost);
   
    $epResult = json_decode($epcall, true);
    if($epResult['status'] != 404){
        
        
    //  $sfStatus = updateSFOpportunity($epResult['client_id'], $epResult['ep_id'] );
        
        if($sfStatus == 1){
        $datamsg = " Success! The sync was a success in both Salesforce and other system. Other System Record " . $epResult['ep_id'] . " was created or updated.<br/>";
        } else {
            $datamsg = " Success! The sync was a success in other system, but failed in Salesforce<br/>";
        }
            echo json_encode(['code'=>200, 'msg'=>$datamsg]);
    } else {
        $datamsg = " Failure! The sync did not work.<br/>";
            echo json_encode(['code'=>404, 'msg'=>$datamsg]);
    } // end epResult




}


$ju = "CustomerData20Fall.json";
 

//read json file from url in php
$readJSONFile = file_get_contents($ju);
 
//convert json to array in php
$jfile = json_decode($readJSONFile);
//print_r($jfile);
//convert json to array in php

$epSync = array();
$oldValue = 0;
$total = count($jfile );

?>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php

if(isset($_REQUEST["NEXTVALUE"])){
    $nextValue = $_REQUEST["NEXTVALUE"];
} else {
    $nextValue = 1;
}

$refreshValue = $nextValue + 20;
$displaycounter = $nextValue;
$timeRemaining = 0;
$updatedRecords = 0;

foreach ($jfile as $key => $jsons) {
    $newKey = $key;
    if($oldValue != $newKey){
        
        if($newKey >= $nextValue  && $newKey < $refreshValue){
        
            //  echo "Updated: " . [$oldValue]['EPID'] . "<br/>";
            //  echo "<hr>" . $nextValue . " >= " . $newKey . " < " . $refreshValue;
            print_r($epSync[$oldValue]); 
        
            $displaycounter = $newKey;
            
            echo sync_systems($epSync[$oldValue]);
            
            usleep(30000);
            
            flush();
            
            } else {
            if($key == ($refreshValue)){
                
            
            $theURL = "sf-ep-sync.php?NEXTVALUE=" . $refreshValue . "&RAND=" . rand();
            //  echo "<hr>"  . $newKey . " = " . $refreshValue . " " . $theURL ."<br/>";
            
            echo "<script>location.href = '" . $theURL . "';</script>";
            exit;
            
                
            }
        }
        
        
        $oldValue = $newKey;
        $i = $nextValue + 1;
        if(($i + 1) == $total ){
            $percent = intval($i/$total * 100)."%";
            $timeRemaining = 0;
        } else {
            $percent = intval($i/$total * 100)."%";
            $timeRemaining = (($total - $displaycounter)/60);
        }
        usleep(30000);
        
    
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$displaycounter.' row(s) of '. $total . ' processed. About ' . round($timeRemaining, 2) . ' minutes remaining.";
   </script>';
        
    

// This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);
    
        
        
    }
     foreach($jsons as $key => $value) {
       $epSync[$newKey][$key] = $value; 
    }
    
}


  


尝试使用众多请求库(如Guzzle)中的一个并行运行主api的1100记录更新。