[英]Get data from a form and send it as a HTTP Request
Okay so, i'm pretty sure this is really easy to do but i just cant make it work. 好的,我很确定这很容易做到,但我不能让它发挥作用。 I dont know how to actually google or form this question so ill try to explain as much as i can.
我不知道如何实际谷歌或形成这个问题,所以我试着解释尽可能多。 I'm trying to use an API from a page and the documentation says that a HTTP request should look like this:
我正在尝试使用页面中的API,文档说HTTP请求应如下所示:
https://example.com/dashboard/api
?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}
&email={YOUR EMAIL}&api_secret={API SECRET}
My goal is to use these parameters and build a basic html form where it asks for the input of 3 different parameters. 我的目标是使用这些参数并构建一个基本的html表单,它要求输入3个不同的参数。
"to", "from" and "message" “to”,“from”和“message”
And then sumbit that information from the form to the http request. 然后将该信息从表单汇总到http请求。
In the documentation they have an example of a JSON response that looks like this: 在文档中,他们有一个JSON响应的示例,如下所示:
{
"to":"4928400837",
"status":"0",
"status_info":"Message sent",
"credits_spent":"1",
"remaining_credits":43,
"id":"username123"
}
Not sure if it helps with anything but thats about it. 不确定它是否有助于解决它的问题。 I have no idea how to begin with this.
我不知道如何开始这个。 Any help is greatly appreciated!
任何帮助是极大的赞赏!
You can create a form using HTML, which you serve to the client, and which sends a POST request on submission to your PHP back-end. 您可以使用HTML创建表单,该表单提供给客户端,并在提交到PHP后端时发送POST请求。
A simple example might look something like this: 一个简单的例子看起来像这样:
<div class="body">
<form method="post" action="index.php">
<div id="form">
<div class="formInput">
<label>To:
<input type="text" name="to" id="to" />
</label>
</div>
<div class="formInput">
<label>From:
<input type="text" name="from" id="from" />
</label>
</div>
<div class="formInput">
<label>Message:
<input type="text" name="message" id="message" />
</label>
</div>
<input type="submit" value="Submit" />
<input type="reset" value="Clear Form" />
</div>
</form>
Your PHP code can then access all of the form variables in the $_POST
array to send the GET request to the API using the $_POST
parameters from your user form. 然后,您的PHP代码可以访问
$_POST
数组中的所有表单变量,以使用用户表单中的$_POST
参数将GET请求发送到API。
<?php
$url = 'https://example.com/dashboard/api';
$r = new HttpRequest($url, HttpRequest::METH_GET);
// build a URL query string with the parameters passed from the form
$r->addQueryData(array('to' => $_POST['to'],
'from' => $_POST['from'],
'email' => $email,
'api_secret' => $api_secret));
// send the HTTP GET request and display the JSON response
try {
$r->send();
if ($r->getResponseCode() == 200) {
echo $r->getResponseBody();
}
} catch (HttpException $ex) {
echo $ex;
}
?>
In this simplistic example, your index.php
would effectively translate the user's POST data into GET data when you hit the API. 在这个简单的示例中,当您点击API时,
index.php
会有效地将用户的POST数据转换为GET数据。 Realistically, you could then redirect the user to a different page that you pass the API's JSON response, so that it can be formatted. 实际上,您可以将用户重定向到您传递API的JSON响应的其他页面,以便可以对其进行格式化。 I wasn't sure if you intended
$email
to be part of the user form, and I'm assuming the API secret must be stored server-side somewhere. 我不确定你是否打算将
$email
作为用户表单的一部分,我假设API秘密必须存储在服务器端的某个地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.