我有一个IP电话,它通过两个动作urls向我的web服务器发出一个get请求。
行动网址:
http://192.168.25.126/action.php?ID=$call_id&remote=$display_remote
http://192.168.25.126/action.php?ID=$call_id&extension=$active_user192.168.25.126是web服务器。
action.php正在将请求打印到日志文件中。
$call_id是电话给当前会话的唯一ID。
$remote是呼叫者的号码
$extension是ip电话分机号码。
在服务器端,我有一个action.php,它通过以下简单的行在日志文件中打印请求
<?php
file_put_contents("/tmp/post.log",print_r($_GET,true), FILE_APPEND));
?>检查日志我可以查看预期的请求
tail -f /tmp/post.log
Array
(
[ID] => 9
[remote] => +39123456789
)
Array
(
[ID] => 9
[extension] => 235
)我如何在tmp.log中合并这样ID相同的拦截器?
Array
(
[ID] => 9
[remote] => +39123456789
[extension] => 235
)要注意的是,第一个arrey是在振铃事件上生成的,而第二个arrey是在我接电话时生成的(或者更好地说,当一个电话建立起来时),所以在第二个时刻,不能用这样的一个动作url来完成这个任务。
http://192.168.25.126/action.php?ID=$call_id&remote=$display_remote&extension=$active_user由于我的ip电话的限制,所以我必须合并2名被捕者。如果可能的话,我希望(但这并不是真正必要的),只有当第一个ID相同的arrey存在时才打印日志(因此,只有在接到来电时,日志才会出现,而不是在我打电话时)。我是一名高级IT人员,不是php程序员,所以我只想要一个建议来写这个循环。非常感谢
发布于 2019-01-05 00:42:36
据我所知,这应该能满足您的需要--期望I是唯一的。
<?php
// get data from log
$fileData = file_get_contents("/tmp/post.log");
$data = json_decode($fileData, true); // make an array out of the json
// $data will now be something like this:
# $data = [["ID" => 9,"remote" => "+39123456789"],["ID" => 10,"remote" => "+41123456789"]];
// mocking input data
# $_GET = ['ID' => 10, 'otherparam' => 'bar'];
$key = array_search($_GET['ID'], array_column($data, 'ID')); // search for pre-saved data
if($key) { // an item with $ID was found -> merge new data
$item = array_merge($data[$key], $_GET);
$data[$key] = $item; // overwrite existing item with this ID
} else {
$item = $_GET; // create a new item, since we haven't found one
$data[] = $item; // append to data
}
file_put_contents("/tmp/post.log",json_encode($data,true))); // don't append, write the whole dataset如果ID不是唯一的,我们可以只获取最后一个添加的ID(通过end() ),检查ID是否匹配并在那里合并:
end($data); // set pointer to the end
$key = key($data); // get the key of the last element
if($data[$key]['ID']==$_GET['ID']) {
$item = array_merge($data[$key], $_GET); // merge old and new data
$data[$key] = $item; // overwrite item
}编辑:
如果您只需要最后一次调用,则不需要重新保存不匹配的数组,因此这段经过调整的代码应该这样做:
<?php
$fileData = file_get_contents("/tmp/post.log");
$data = json_decode($fileData, true);
// $data will now be something like this:
# $data = ["ID" => 9,"remote" => "+39123456789"]; // note, this time it's a one-dimentional (but assoc) array.
// mocking input data
# $_GET = ['ID' => 9, 'otherparam' => 'bar'];
// check if we have pre-saved data, that has an 'ID' and that matches our current one:
if(is_array($data) && isset($data['ID']) && $data['ID']==$_GET['ID']) { // the saved $ID was found -> merge new data
$data = array_merge($data, $_GET);
} else {
$data = $_GET; // create a new item, since we haven't found one
}
file_put_contents("/tmp/post.log",json_encode($data,true))); // don't append, write the whole dataset免责声明:这段代码没有任何错误检查,并会在空白日志上抛出错误(因为如果还没有数据,json_decode将失败),存在一些安全问题(使用$GET而不对其进行消毒并将其写入文件.),不会测试是否有正确的输入(如果没有发送ID怎么办),等等.
https://stackoverflow.com/questions/54047565
复制相似问题