[英]Sending json posts from WordPress to an android app
Hope someone can help.希望有人能帮忙。 I am using an app that sends HTTP POST requests.I have a Wordpress site and i want to host a list numberplates that we are going to call denied plates
我正在使用一个发送 HTTP POST 请求的应用程序。我有一个 Wordpress 网站,我想托管一个我们将称为拒绝车牌的车牌列表
the following is from the app developer:以下内容来自应用程序开发人员:
**Request The android device sends a HTTP POST message to the given url + /api/bwlist path. **请求 android 设备向给定的 url + /api/bwlist 路径发送 HTTP POST 消息。 The app generates a regular HTTP multipart message with the following fields:
该应用程序生成一条包含以下字段的常规 HTTP 多部分消息:
• nationality: The plate's nationality • plate: the plate text • 国籍:车牌的国籍 • 车牌:车牌文字
This is an HTML that generates the same message:这是生成相同消息的 HTML:
<!DOCTYPE html>
<html>
<body>
<form action="http://ip:port/api/bwlist" method="post"
enctype="multipart/form-data">
<label>Plate</label>
<input type="text" name="plate" value="ARH001"><br>
<label>Nat</label>
<input type="text" name="nationality" value="HUN"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Result
For such a request, the server responds with a JSON text. Currently it has only one field: “type”.
The value can be “deny”, “allow” or “none” (lowercase). An example of such a result is the
following:
{
“type”:”deny”
}**
can someone please explain in simple terms how i can create an api that sends a response if the plate sent in the HTTP POST is in a denied list its not someting ive ever dne before but im good with wordpress to a limited level.有人可以用简单的术语解释一下我如何创建一个 api 来发送响应,如果在 HTTP POST 中发送的盘子在拒绝列表中它不是我以前做过的事情但我对 wordpress 很好用到一个有限的水平。 TIA
TIA
So we'll start with the database part.所以我们将从数据库部分开始。 you'll need to set up a database table to store the data.
您需要设置一个数据库表来存储数据。 feel free to alter the MYSQL data types
随意更改 MYSQL 数据类型
so we're using wordpress dbDelta()
function and a table that stores all the vehicle info as well as a status (feel free to alter table columns)所以我们使用 wordpress
dbDelta()
function 和一个存储所有车辆信息以及状态的表(随意更改表列)
function vehicles_database_functions(){
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name_vehicles = $wpdb->prefix . 'vehicles';
$table_name_vehicle_status = $wpdb->prefix . 'vehicle_status';
$vehicles_sql = "CREATE TABLE " . $table_name_vehicles . " (
id int(20) NOT NULL AUTO_INCREMENT,
numberplate_country_code varchar(3) NOT NULL,
numberplate varchar(10) NOT NULL,
type char(5) NOT NULL,
date_created timestamp NOT NULL,
date_modified timestamp NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($vehicles_sql);
}
vehicles_database_functions();
now that we have the database set up you can fill it with vehicle content.现在我们已经设置了数据库,您可以用车辆内容填充它。
next you'll want to handle the HTTP request:接下来你要处理 HTTP 请求:
so make a new file in your theme root called plate_callback.php
所以在你的主题根目录中创建一个名为
plate_callback.php
的新文件
inside that file you'll handle all the HTTP request: (make sure you set up some sort of verification to know whos posting to your website is who it says it is.在该文件中,您将处理所有 HTTP 请求:(确保您设置了某种验证,以了解谁在您的网站上发帖就是它所说的是谁。
$country_code = isset( $_POST['country_code'] ) ? sanitize_text_field( $_POST['country_code'] ) : '';
$numberplate = isset( $_POST['numberplate'] ) ? sanitize_text_field( $_POST['numberplate'] ) : '';
$type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '';
if( $country_code && $numberplate && $type ){
global $wpdb;
$table_name_vehicles = $wpdb->prefix . 'vehicles';
$denied_vehicle = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM $table_name_vehicles WHERE numberplate_country_code=%s AND numberplate=%s AND denied_entry=%s", array( $country_code, $numberplate, $type ) ), ARRAY_A
);
if( $denied_vehicle ){
$vehicle_status = array('type' => $denied_vehicle['type']);
echo json_encode($vehicle_status);
}
else{
return false;
}
}
else{
wp_die('Error Message');
}
This should get you started off in the right direction这应该让你开始朝着正确的方向
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.