将数据从表单发送到 http post 请求但格式错误?

白衣染霜花
浏览 166回答 2
2回答

守候你守候我

我正在尝试发送发布请求,这是正确的格式https://domainname.com/dashboard/api?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}&email={YOUR EMAIL}&api_secret={API SECRET}这是请求的样子:https://domainname.com/dashboard/api?to=123456789&from=text&message=text&email=email@email.com&api_secret=123abc所以我做了一个html表单:<div class="body">&nbsp;&nbsp; &nbsp; <form method="post" action="index.php">&nbsp; &nbsp; &nbsp; &nbsp; <div id="form">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="formInput">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>To:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="to" id="to" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="formInput">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>From:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="from" id="from" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="formInput">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Message:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="message" id="message" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="formInput">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Email:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="email" id="email" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="formInput">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Api_Secret:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="api_secret" id="api_secret" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Submit" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; </form>还有一个用 curl 处理数据的 php 文件:<?php$ch = curl_init();curl_setopt_array($ch, [&nbsp; &nbsp; CURLOPT_URL => 'https://domainname.com/dashboard/api',&nbsp; &nbsp; CURLOPT_RETURNTRANSFER => true,&nbsp; &nbsp; CURLOPT_POST => true,&nbsp; &nbsp; CURLOPT_POSTFIELDS => [&nbsp; &nbsp; &nbsp; &nbsp; 'to' => $_POST['to'],&nbsp; &nbsp; &nbsp; &nbsp; 'from' => $_POST['from'],&nbsp; &nbsp; &nbsp; &nbsp; 'message' => $_POST['message'],&nbsp; &nbsp; &nbsp; &nbsp; 'email' => $_POST['email'],&nbsp; &nbsp; &nbsp; &nbsp; 'api_secret' => $_POST['api_secret'],&nbsp; &nbsp; ],]);$response = curl_exec($ch);curl_close($ch);echo($response);?>但它仍然不起作用。我在 postbin 上做了一个请求,查询如下所示:123456789:text:text:email@email.com:123abc:是不是格式不对?我是否以错误的格式发送所有内容?非常感谢您的帮助,因为我在过去 3 天里一直在搞砸这个..

湖上湖

您的API似乎接受 GET 请求而不是 POST,您使用 curl的调用是 POST。因此,您的表单似乎没问题,它们具有 API 所需的所有变量。现在我认为问题出在你的 index.php 文件(从表单中调用),试试这个:<?php//checking for all variables filled in formif (isset($_POST['to']) && isset($_POST['from']) && isset($_POST['message']) && isset($_POST['email']) && isset($_POST['api_secret'])){&nbsp; &nbsp; //rebuild API call&nbsp; &nbsp; $_ENDPOINT_CALL = "https://domainname.com/dashboard/api?to={$_POST['to']}&from={$_POST['from']}&message={$_POST['message']}&email={$_POST['email']}&api_secret={$_POST['api_secret']}";&nbsp; &nbsp; //cURL GET request&nbsp; &nbsp; $curl = curl_init();&nbsp; &nbsp; curl_setopt_array($curl, [&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_RETURNTRANSFER => 1,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_URL => $_ENDPOINT_CALL&nbsp; &nbsp; ]);&nbsp; &nbsp; $response = curl_exec($curl);&nbsp; &nbsp; curl_close($curl);&nbsp; &nbsp; //write response&nbsp; &nbsp; echo $response;}?>
打开App,查看更多内容
随时随地看视频慕课网APP