如何将两个 PHP' include' 合并到一个 PHP 'include' a并用于 PHP 页面



伙计们,我目前正在用PHP构建一个网站。我使用网络服务(url(将我的网站连接到数据库。系统也在运行。然后,我通过创建其他PHP文件来分离web服务,放置web服务并将PHP文件包含到我的网站中。这也运行得很好。以下是代码:

add_factory.php

<?php
include("../../config/check.php");
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
include("../../api/api_add_factory.php"); //include web services 1
if(empty($queryt)){
include("../../api/api2_add_factory.php"); //include web services 2
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
?>

api_add_factory.php

<?php
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$data = file_get_contents($url);
$json = json_decode($data);
$queryt = $json->factoryList;
?>

api2_add_factory.php

<?php
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
$data2 = file_get_contents($url2);
$json2 = json_decode($data2);
?>

当您在"add_factory.php"中看到时,有两个include。现在,我只想使用一个可以调用"api_add_filtery.php"one_answers"api2_add_factory.php"的include。但我不知道如何做到这一点,因为"add_factory.php"中的"include"位于不同的位置。

有人知道如何将两个include文件合并为一个吗?我需要在"add_factory.php"中的哪个位置创建include文件?

我的建议是将api_add_factory.phpapi2_add_factory.php的内容重写为函数。这将允许您控制包含它们的代码段如何以及何时调用它们,而不是在调用include时立即执行。此外,根据一般经验,include都应该在文件的开头调用,因为否则它们可能会变得很难跟踪。

所以,我想说你可以这样做:

api_add_factory.php

<?php
function callApi1()
{
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$data = file_get_contents($url);
$json = json_decode($data);
return $json->factoryList;
}

api2_add_factory.php

<?php
function callApi2()
{
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
$data2 = file_get_contents($url2);
return json_decode($data2);
}

add_factory.php

<?php
include("../../config/check.php");
include("../../api/api_add_factory.php"); //include web services 2
include("../../api/api2_add_factory.php"); //include web services 2
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
$queryt = callApi1();
if(empty($queryt)){
$json2 = callApi2();
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}

事实上,现在我正在查看我的答案,我可以看到进一步改进的可能性:由于api_add_factory.phpapi2_add_factory.php的内容实际上是相同的,除了被调用的url之外,你可以做这样的事情:

api_add_factory.php

<?php
function callApi($url)
{
$data = file_get_contents($url);
return json_decode($data);
}

add_factory.php

<?php
include("../../config/check.php");
include("../../api/api_add_factory.php"); //include web services
$url1 = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
$json1 = callApi($url1);
$queryt = $json1->factoryList;
if(empty($queryt)){
$json2 = callApi($url2);
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}

这样,我们就不需要重复函数的代码了。

相关内容

  • 没有找到相关文章

最新更新