我的问题是我有一个表单,我想将其值提交到两个位置,1表示我的网络数据库,另一个表示接受此api的vicidial服务器,这样做的方法是什么,我是在插入数据库之后使用header()
思考php重定向,这不是很麻烦吗?还有其他方法吗?
表格 - >数据库 - >这个apilink
http://serverip/vicidial/non_agent_api.php?phone_number=123456&first_name=John&last_name=Smith&address1=7153+Marbella+Unit+401&city=Cape+Canaveral&state=FL&postal_code=329200000&custom_fields=Y&birth_date=12-25-2017&duplicate_check=DUPCAMP
答案 0 :(得分:1)
有两种选择:
首先使用ajax调用将表单数据提交到您的应用服务器,然后将相同的数据提交给第三方API端点。
您只需将表单提交到您的应用服务器,然后从您的应用服务器(在数据库中存储数据后)向第三方API发送请求(可能使用curl)。
我个人更喜欢第二个。因为它没有向用户公开供应商API。
答案 1 :(得分:0)
您可以使用file_get_contents:
let net = require('net');
let fs = require('fs');
let client = new net.Socket();
const IMAGE_HEADER_SIZE = 64;
const IMAGE_PREFIX_SIZE = 16;
const STATE_INIT = 0;
const STATE_DATA = 1;
let state = STATE_INIT;
let imageData;
let imageOffset = 0;
let headerImageSizeBytes = 0;
let headerImageFrameNumber = 0;
let imageBytesNeeded = 0;
let bytesRead = 0;
client.connect(32200, '192.168.0.20', function ()
{
client.setNoDelay(true);
console.log('connected');
});
client.on('data', function (buffer) {
switch (state) {
case STATE_INIT:
// Ideally we'd have a more robust test here, i.e. consider scenario
// where header and image data arrive in same buffer
if (buffer.length !== 64) {
console.error('unexpected data received (bytes: ' + buffer.length + ')');
return;
}
console.log(bytesRead == 0 ?
"Acquiring next image" :
"Last Image Bytes read = " + bytesRead);
bytesRead = 0;
headerImageSizeBytes = 0;
// Parse the header (Don't need 1st 16 Prefix bytes at this time)
var offset = IMAGE_PREFIX_SIZE;
headerImageSizeBytes = buffer.readUInt32LE(offset += 4);
headerImageFrameNumber = buffer.readUInt32LE(offset += 4);
imageBytesNeeded = headerImageSizeBytes;
imageData = Buffer.alloc(imageBytesNeeded)
imageOffset = 0;
state = STATE_DATA;
break;
case STATE_DATA:
imageOffset += buffer.copy(imageData, imageOffset);
if (imageOffset >= imageBytesNeeded) {
fs.appendFile('c:/temp/_FTP_FOLDER/image' + headerImageFrameNumber + '.bmp', imageData, function (err) {
if (err) {
console.log(err.message);
}
});
state = STATE_INIT;
} // else continue acquiring image until all bytes received
break;
}
});
client.on('end', function () {
console.log('Transimission end');
});
client.on('close', function () {
console.log('Connection closed');
});
否则,您在接收到标题指令后依赖于客户端的浏览器进行重定向,您还将暴露终点,并且易于进行数据操作。
答案 2 :(得分:0)
您可以创建两个函数,一个用于将提交的数据保存到数据库,另一个用于将其提交给API。
function handleForm()
{
$data = [
phone_number => $_POST['phone_number'],
first_name => $_POST['first_name'],
// etc ...
];
$inserted = insertToDb($data);
if ($inserted) {
$api_response = submitToApi($data);
}
}
function insertToDb($data)
{
$inserted = false;
// do stuff to insert data to database
if (/**stuff went good**/) {
$inserted = true;
}
return $inserted;
}
function submitToApi($data)
{
$query_string = http_build_query($data);
$url = 'http://serverip/vicidial/non_agent_api.php?' . $query_string;
return file_get_contents($url);
}