期望从' $数组中提取JSON对象'通过'foreach'并使用cURL每次发布当前数据对象。
目前收到错误500并不完全确定原因。
<?php
$string = file_get_contents("/home/scott/sandbox/apitest/export.json");
$array = json_decode($string, true);
foreach ($array as $group) {
$Name = $group['Name'];
$Position = $group['Position'];
$Company = $group['Company'];
$Description = $group['Description'];
$Country = $group['Country'];
$Zip = $group['Zip'];
$City = $group['City'];
$State = $group['State'];
$Address = $group['Address'];
$Email = $group['Email'];
$Website = $group['Website'];
$Phonenumber = $group['Phonenumber'];
$CompanyNumber = $group['CompanyNumber'];
$CompanyName = $group['CompanyName'];
$RegAddressAddressLine1 = $group['RegAddressAddressLine1'];
$RegAddressAddressLine2 = $group['RegAddressAddressLine2'];
$RegAddressPostTown = $group['RegAddressPostTown'];
$RegAddressCounty = $group['RegAddressCounty'];
$RegAddressCountry = $group['RegAddressCountry'];
$RegAddressPostCode = $group['RegAddressPostCode'];
$CompanyCategory = $group['CompanyCategory'];
$CompanyStatus = $group['CompanyStatus'];
$CountryOfOrigin = $group['CountryOfOrigin'];
$DissolutionDate = $group['DissolutionDate'];
$IncorporationDate = $group['IncorporationDate'];
$URI = $group['URI'];
$myobj = array(
"data" => array(
"Name" => $Name,
"Position" => $Position,
"Company" => $Company,
"Description" => $Description,
"Country" => $Country,
"Zip" => $Zip,
"City" => $City,
"State" => $State,
"Address" => $Address,
"Email" => $Email,
"Website" => $Website,
"Phonenumber" => $Phonenumber,
"CompanyNumber" => $CompanyNumber,
"CompanyName" => $CompanyName,
"RegAddressAdressLine1" => $RegAddressAdressLine1,
"RegAddressAdressLine2" => $RegAddressAdressLine2,
"RegAddressPostTown" => $RegAddressPostTown,
"RegAddressCounty" => $RegAddressCounty,
"RegAddressCountry" => $RegAddressCountry,
"RegAddressPostCode" => $RegAddressPostCode,
"CompanyCategory" => $CompanyCategory,
"CompanyStatus" => $CompanyStatus,
"CountryOfOrigin" => $CountryOfOrigin,
"DissolutionDate" => $DissolutionDate,
"IncorporationDate" => $IncorporationDate,
"URI" => $URI
)
);
curl_setopt_array($curl, array(
CURLOPT_URL => "http://xx.xx.xx.xx:3001/derp/submission",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $myobj,
CURLOPT_HTTPHEADER => array(
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
// if ($err) {
// echo "cURL Error #:" . $err;
// } else {
// echo $response;
// }
}
?>
答案 0 :(得分:2)
尝试替换:
CURLOPT_POSTFIELDS => $myobj,
使用:
CURLOPT_POSTFIELDS => json_encode($myobj),
您正在尝试将PHP数组发送到服务器,Content-Type标头为application / json,因此您应将其编码为json。