我试图向API发送一个PUT请求,以便将在HTML表单中提交的值添加到API中。
if ($_SERVER["REQUEST_METHOD"] == "POST") {
function ediprinterMarcacao(){
$name = trim($_POST["name"]);
$dataMarcacao = trim($_POST["data-marcacao"]);
$horaMarcacao = trim($_POST["hora-marcacao"]);
//$dataMarcacao = ($_POST["data-marcacao"]);
//$horaMarcacao = ($_POST["hora-marcacao"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$centro = ucfirst(trim($_POST["centro-inspecoes"]));
$matricula = trim($_POST["matricula"]);
$telefone = trim($_POST["telefone"]);
$tipoInspecao = trim($_POST["tipo-inspecao"]);
$tipoViatura = trim($_POST["tipo-viatura"]);
$comentario = trim($_POST["comentario"]);
$formatDate = date("Y-m-d", strtotime($dataMarcacao));
$dataM = str_replace(array('-'), '',$formatDate);
$horaM = str_replace(array(':'), '',$horaMarcacao);
if($centro == "Paredes"){
//$centroID = 0;
//$tag = 25;
}
elseif ($centro == "Braga"){
//$centroID = 1;
//$tag = 26;
}
if($tipoViatura == "LIGEIRO"){
$tipoViaturaId = 1;
//$centroID = 0;
//$tag = 25;
}
elseif($tipoViatura == "PESADO"){
$tipoViaturaId = 2;
//$centroID = 1;
//$tag = 26;
}
elseif($tipoViatura == "TRACTOR"){
$tipoViaturaId = 3;
}
elseif($tipoViatura == "REBOQUE"){
$tipoViaturaId = 6;
}
elseif($tipoViatura == "SEMI-REBOQUE"){
$tipoViaturaId = 7;
}
elseif($tipoViatura == "TRACTOR < 3500Kg"){
$tipoViaturaId = 9;
}
if($tipoInspecao == "Inspecção Periódica"){
$tipoInspecaoId = 1;
}
elseif($tipoInspecaoId == "Reinspecção"){
$tipoInspecaoId = 2;
}
elseif($tipoInspecaoId == "Insp.p/atrib.nova Matricula"){
$tipoInspecaoId = 3;
}
elseif($tipoInspecaoId == "Inspecção Extraordinária"){
$tipoInspecaoId = 4;
}
elseif($tipoInspecaoId == "Inspecção Facultativa"){
$tipoInspecaoId = 5;
}
$curl = curl_init();
$fields = array($dataM, $horaM, $matricula, $name, $email, $telefone, $tipoViatura, $tipoInspecao);
$teste = http_build_query($fields, null, "/");
$removeNum = array("0=", "1=", "2=", "3=", "4=", "5=", "6=", "7=");
$teste1 = str_replace($removeNum, "", $teste);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://edigcia.ediprinter.pt/edigciaappointmentapi/InsertAppointment188/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $teste1,
//CURLOPT_POSTFIELDS => $fields,
CURLOPT_HTTPHEADER => array(
"Accept: multipart/form-data",
"Content-Type: multipart/form-data",
),
));
echo $curl;
$response = curl_exec($curl);
echo "<br>";
echo $curl;
echo "<br>";
curl_close($curl);
echo $response;
}
return ediprinterMarcacao();
}我感觉我的curl请求没有被很好地完成,因为它返回了这个错误消息:enter image description here
我已经将我的代码与其他示例进行了比较,它看起来很好,所以我需要在这个问题上有第二个意见,这样我才能发现我做错了什么。
发布于 2021-02-19 23:00:54
感谢大家的评论。我终于设法把它弄对了。当涉及到这个过程时,我是一个新手,所以我花了一段时间来理解它。我还将发布工作代码。
function ediprinterMarcacao(){
$name = trim($_POST["name"]);
$dataMarcacao = trim($_POST["data-marcacao"]);
$horaMarcacao = trim($_POST["hora-marcacao"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$centro = ucfirst(trim($_POST["centro-inspecoes"]));
$matricula = trim($_POST["matricula"]);
$telefone = trim($_POST["telefone"]);
$tipoInspecao = trim($_POST["tipo-inspecao"]);
$tipoViatura = trim($_POST["tipo-viatura"]);
$comentario = trim($_POST["comentario"]);
$formatDate = date("Y-m-d", strtotime($dataMarcacao));
$dataM = str_replace(array('-'), '',$formatDate);
$horaM = str_replace(array(':'), '',$horaMarcacao);
if($centro == "Paredes"){
}
elseif ($centro == "Braga"){
}
if($tipoViatura == "LIGEIRO"){
$tipoViaturaId = 1;
}
elseif($tipoViatura == "PESADO"){
$tipoViaturaId = 2;
}
elseif($tipoViatura == "TRACTOR"){
$tipoViaturaId = 3;
}
elseif($tipoViatura == "REBOQUE"){
$tipoViaturaId = 6;
}
elseif($tipoViatura == "SEMI-REBOQUE"){
$tipoViaturaId = 7;
}
elseif($tipoViatura == "TRACTOR < 3500Kg"){
$tipoViaturaId = 9;
}
if($tipoInspecao == "Inspecção Periódica"){
$tipoInspecaoId = 1;
}
elseif($tipoInspecaoId == "Reinspecção"){
$tipoInspecaoId = 2;
}
elseif($tipoInspecaoId == "Insp.p/atrib.nova Matricula"){
$tipoInspecaoId = 3;
}
elseif($tipoInspecaoId == "Inspecção Extraordinária"){
$tipoInspecaoId = 4;
}
elseif($tipoInspecaoId == "Inspecção Facultativa"){
$tipoInspecaoId = 5;
}
$curl = curl_init();
/*$fields = array(
'date' => $dataM,
'hour' => $horaM,
'licensePlate' => $matricula,
'name' => $name,
'email' => $email,
'phone' => $telefone,
'category' => $tipoViatura,
'motive' => $tipoInspecao
);*/
$fields = array($dataM, $horaM, $matricula, $name, $email, $telefone, $tipoViatura, $tipoInspecao);
//code formats the url in the way it needs to be sent
$buildURL = http_build_query($fields, null, "/");
$removeNum = array("0=", "1=", "2=", "3=", "4=", "5=", "6=", "7=");
$path = str_replace($removeNum, "", $buildURL);
$url = "https://edigcia.ediprinter.pt/edigciaappointmentapi/InsertAppointment188/" . $path;
echo $url;
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Content Length: 0",
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
return ediprinterMarcacao();https://stackoverflow.com/questions/66277850
复制相似问题