0

Good day I'm trying to create some virtual cards with an array of json from a CSV file. The CSV file have 100 cards that I'm trying to create. If I try to post the full 100 lines in the csv file I get a 504 error. If I post it with only 10 lines the post is successful printing the response.

I'm hosting this on a shared hosting account. I cannot change the http.conf or any other apache configuration.

My CSV file

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

My Code. . .

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

My echoed 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

I have tried to understand this. Such as putting this in data chunks or using stream_context_create. Is there a better way to do this such as 10 lines at a time?

2 Answers2

0

504 is the gateway timeout error. Since your code is sending requests sequentially, the php script is probably taking too long to answer. You may try to print something and flush() it to the client or make the curl requests in parallel.

You may use curl_multi to run the curl requests in parallel. There is a code example here: PHP Parallel curl requests

Enrico Dias
  • 1,417
  • 9
  • 21
0

I had a similar issue, but it wasn't PHP related at all. After I'd increased the timeouts in my PHP code, I was still getting it, until I realized I needed to add

Timeout 180

to /etc/httpd/conf/httpd.conf for Apache.

hemisphire
  • 1,205
  • 9
  • 19