首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP的PUT请求接口

PHP的PUT请求接口
EN

Stack Overflow用户
提问于 2021-02-19 20:47:23
回答 1查看 38关注 0票数 0

我试图向API发送一个PUT请求,以便将在HTML表单中提交的值添加到API中。

代码语言:javascript
复制
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

我已经将我的代码与其他示例进行了比较,它看起来很好,所以我需要在这个问题上有第二个意见,这样我才能发现我做错了什么。

EN

回答 1

Stack Overflow用户

发布于 2021-02-19 23:00:54

感谢大家的评论。我终于设法把它弄对了。当涉及到这个过程时,我是一个新手,所以我花了一段时间来理解它。我还将发布工作代码。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66277850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档