我正在一个朋友的自助存储网站上工作。他们将WebSelfStorage用于租户管理软件。可以在https://api.webselfstorage.com/
找到api文档。我已经能够成功检索所需的信息。您可以在此处测试代码:http://broken.press/testing/hydry/。我已经能够将选定的信息发送到预订页面,但是当您去设置预订时,会出现此错误:
array(4){[“ Parameter”] => string(14)“ ReservationDay” [“ Reason”] => string(97)“无法识别的日期格式。请使用位置请求中提供的日期时间字符串之一“ [[”成功“] => bool(false)[” ErrorMessage“] => string(34)“指定了无效的参数”}
根据文档,似乎它需要ISO8601 DateTime格式,因此我以这种方式对其进行了格式化(当前代码)。我还设置了格式以匹配而不是将其转换为ISO8601(也不起作用)。下面是代码(出于隐私目的删除了用户名,密码和授权)。
SubmitReservation.php
<body>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php //Submit Reservation
$username = XXXXXX;
$password = XXXXXX;
$url = "https://api.webselfstorage.com/wssapi/v2/reservation/1025140";
$header = array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic XXXXXXX',
);
$dateFormat = 'n/j/Y';
$dateString = $_POST['moveIn'];
$date = date(DATE_ISO8601, strtotime($dateString));
if ($_POST['mobile'] = ""){
$phone = $_POST['home'];
} else {
$phone = $_POST['mobile'];
}
$data_array = array(
"FirstName" => $_POST['fname'],
"LastName" => $_POST['lname'],
"Phone" => $phone,
"Address1" => $_POST['street'],
"City" => $_POST['city'],
"State" => $_POST['state'],
"Zip" => $_POST['zip'],
"Email" => $_POST['email'],
"UnitID" => $_POST['unitID'],
"MoveInDate" => $date,
"ReservationDay" => $date //<< Edited
);
/*$reservationOptions = new stdClass();
$reservationOptions->firstName = $_POST['fname'];
$reservationOptions->lastName = $_POST['lname'];
$reservationOptions->email = $_POST['email'];
$reservationOptions->mobilePhone = $_POST['mobile'];
$reservationOptions->homePhone = $_POST['home'];
$reservationOptions->street1 = $_POST['street'];
$reservationOptions->city = $_POST['city'];
$reservationOptions->state = $_POST['state'];
$reservationOptions->zip = $_POST['zip'];
$reservationOptions->moveinDate = $_POST['moveIn'];
$reservationOptions->unitTypeId = $_POST['unit-name'];*/
// Make API Call
function callAPI($method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic 2f5a1329-43f9-40d6-93c5-8e36a00ef725',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}
$make_call = callAPI('POST', 'https://api.webselfstorage.com/wssapi/v2/reservation/1025140', json_encode($data_array));
$response = json_decode($make_call, true);
$errors = $response['response']['errors'];
var_dump($response);?><br/>
<?php
print_r($response);
echo $date;
?><br/>
Thank you for your request. You will be redirected in just a second.
<script>
//setTimeout(function () {
// window.top.location.href = "http://www.google.com"; //will redirect to your blog page (an ex: blog.html)
//}, 2000);
</script>
</body>
</html>
Reservation.php(表格)
<title>Reservation Form</title>
<?php
$unitName = $_POST['unitname'];
$sessionId = $_SESSION['sessionId'];
$facility = "1025140";
$unitID = $_POST['unitID'];
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
$(document).ready(function(){
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
$("#datepicker").datepicker({
beforeShowDay: nonWorkingDates,
numberOfMonths: 1,
minDate: today,
maxDate: '+2M',
firstDay: 0
});
function nonWorkingDates(date){
var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
var closedDates = [[7, 4, 2019]];
var closedDays = [[Sunday]];
for (var i = 0; i < closedDays.length; i++) {
if (day == closedDays[i][0]) {
return [false];
}
}
for (i = 0; i < closedDates.length; i++) {
if (date.getMonth() == closedDates[i][0] - 1 &&
date.getDate() == closedDates[i][1] &&
date.getFullYear() == closedDates[i][2]) {
return [false];
}
}
return [true];
}
});
</script>
</head>
<body>
<div class="form_container">
<form id="reservation" name="submitReservation.php" method="post">
<div class="form-title"><p>Apply For This Unit</p></div>
<div class="form-description"><p>Fill out this form, and a manager will be in touch shortly to confirm your move-in date.
Applying for:<div class="unit_type"><?php echo $unitName;?></p><input type="hidden" value="<?php echo $unitID;?>" name="unit-name" id="unit-name"></div></p></div>
<p>
<input type="text" name="fname" id="fname" placeholder="First Name">
<input type="text" name="lname" id="lname" placeholder="Last Name"><br/>
<input type="email" name="email" id="email" placeholder="Email Address"><br/>
<div class="form-sub">Phone Numbers:</div><br/>
<input type="tel" name="mobile" id="mobile" placeholder="Cell Phone"><br/>
<input type="tel" name="home" id="home" placeholder="Home Phone"><br/>
<div class="form-sub">Address:</div><br/>
<input type="text" name="street" id="street" placeholder="Street Address"><br/>
<input type="text" name="city" id="city" placeholder="City"><br/>
<select name="state" id="state" >
//Redacted all 50 state options for space
</select>
<input type="text" name="zip" id="zip" placeholder="Zip">
<br/>
<br/>
<input type="text" name="moveIn" id="datepicker" placeholder="Move-In Date"> <br/>
<script type="text/javascript">
</script>
<button type="submit" formmethod="post" formaction="submitReservation.php">Set Reservation</button>
</p>
</div>
</form>
我只需要通过API将预订表中的信息添加到WebSelfStorage预订系统中即可。
答案 0 :(得分:0)
您应该更改 “内容类型:应用程序/ x-www-form-urlencoded”, 至 内容类型:application / json