【发布时间】: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