【问题标题】:Why PHP Curl with a while loop produces a 504 Timeout?为什么带有while循环的PHP Curl会产生504超时?
【发布时间】:2020-04-03 21:53:55
【问题描述】:

美好的一天,我正在尝试使用 CSV 文件中的 json 数组创建一些虚拟卡。 CSV 文件有 100 张我正在尝试创建的卡片。如果我尝试在 csv 文件中发布完整的 100 行,则会收到 504 错误。如果我只用 10 行发布它,则该帖子会成功打印响应。

我将其托管在共享托管帐户上。我无法更改 http.conf 或任何其他 apache 配置。

我的 CSV 文件

First,Lasy,DOB,Phone,Email,Address 1,Address 2,City,State,Zip,Country
John,Doe,1/1/2000,5555555555,something@example.com,123 something street,,Atlanta,GA,00000,US
Jimmy,Doe,1/1/2000,5555555555,something@example.com,123 something street,,Atlanta,GA,00000,US

我的代码。 . .

$handle = fopen('sgvc.csv', 'r')
$header = fgetcsv($handle);
while(($data = fgetcsv($handle)) !== false){

        $sv = '{
      "VirtualCards":[{  
      "FirstName":"'.$data[0].'",  
     "LastName":"'.$data[1].'",  
     "DateOfBirth":"'.$data[2].'", 
    "Phone":"'.$data[3].'", 
     "Email":"'.$data[4].'", 
     "ProfileAddress":{  
      "AddressLine1":"'.$data[5].'", 
       "AddressLine2":"'.$data[6].'", 
       "City":"'.$data[7].'", 
       "State": "'.$data[8].'", 
       "PostalCode":"'.$data[9].'", 
       "Country":"'.$data[10].'", 
       }, 
      "GroupId": ""}]

     }';

        echo $sv;
       $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,  $sv);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 900);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: token ','Content-Type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);
    }
    fclose($handle);
}

我的回声 json..

    { "VirtualCards":[{ "FirstName":"John", "LastName":"Doe", "DateOfBirth":"2/3/1981", "Phone":"5555555555", "Email":"something@example.com", "ProfileAddress":{ "AddressLine1":"123something rd", "AddressLine2":"", "City":"atlanta", "State": "GA", "PostalCode":"00000", "Country":"US", }, "GroupId": "123",}] }
{ "VirtualCards":[{ "FirstName":"John", "LastName":"Doe", "DateOfBirth":"2/3/1981", "Phone":"5555555555", "Email":"something@example.com", "ProfileAddress":{ "AddressLine1":"123something rd", "AddressLine2":"", "City":"atlanta", "State": "GA", "PostalCode":"00000", "Country":"US", }, "GroupId": "123",}] } //...array of json

我试图理解这一点。例如将其放入数据块或使用 stream_context_create。有没有更好的方法来做到这一点,比如一次 10 行?

【问题讨论】:

    标签: php curl http-post php-curl fgetcsv


    【解决方案1】:

    504 是网关超时错误。由于您的代码按顺序发送请求,因此 php 脚本可能需要很长时间才能回答。您可以尝试打印一些内容并将其flush() 发送给客户端或并行发出 curl 请求。

    您可以使用curl_multi 并行运行 curl 请求。这里有一个代码示例:PHP Parallel curl requests

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 2016-07-24
      • 2013-03-29
      • 2021-08-17
      • 1970-01-01
      • 2012-11-07
      • 2016-03-02
      • 2019-10-17
      • 2013-06-19
      相关资源
      最近更新 更多