编写对php的调用的最佳方法 [英] Best way to program a call to php
问题描述
我最近在这里发布了使用时访问$ _SESSION PHP中的file_get_contents 关于我遇到的一个问题,并且普遍的共识是我做的不正确...而我通常认为只要它能起作用...",我想我会得到一些关于如何做得更好的反馈...
I've recently posted here accessing $_SESSION when using file_get_contents in PHP about a problem I was having and the general consensus is that I'm not doing it right... while I generally think "as long as it works..." I thought I'd get some feedback on how I could do it better...
我要从多个不同的区域以完全相同的格式发送完全相同的电子邮件.
I was to send the exact same email in the exact same format from multiple different areas.
- 输入作业后(自动作为POST的一部分)
- 在查看作业以将其重新分配给其他安装程序时手动
原始脚本是一个php页面,该页面使用AJAX来发送以发送工作指令请求-只需调用一个标准php页面,返回成功或错误消息,然后在调用页面中显示即可.
The original script is a php page which is called using AJAX to send the work order request - this worked by simply calling a standard php page, returning the success or error message and then displaying within the calling page.
现在,我尝试在自动作业条目中使用同一页面,以便它通过表单接受作业,记录并通过邮件发送.
Now I have tried to use the same page within the automated job entry so it accepts the job via a form, logs it and mails it.
我的问题是(如您在原始文章中所见),函数file_get_contents()不适用于自动脚本中的此原因...
My problem is (as you can see from the original post) the function file_get_contents() is not good for this cause in the automated script...
我的问题是,在AJAX调用中,我需要做一些事情,包括数据库连接初始化程序,启动会话以及在独立页面中做其他需要做的事情……如果不需要某些或全部这些,则不需要它是一个include文件,因此使该文件仅出于一种目的而有用...
My problem is that from an AJAX call I need to do things like include the database connection initialiser, start the session and do whatever else needs to be done in a standalone page... Some or all of these are not required if it is an include so it makes the file only good for one purpose...
如何使文件同时满足这两个目的?我想我正在寻找针对两种情况的最佳文件布局和结构的建议...
How do I make the file good for both purposes? I guess I'm looking for recommendations for the best file layout and structure to cater for both scenarios...
当前文件如下:
<?php
session_start();
$order_id = $_GET['order_id'];
include('include/database.php');
function getLineItems($order_id) {
$query = mysql_query("SELECT ...lineItems...");
//Print rows with data
while($row = mysql_fetch_object($query)) {
$lineItems .= '...Build Line Item String...';
}
return $lineItems;
}
function send_email($order_id) {
//Get data for current job to display
$query = mysql_query("SELECT ...Job Details...");
$row = mysql_fetch_object($query);
$subject = 'Work Order Request';
$email_message = '...Build Email...
...Include Job Details...
'.getLineItems($order_id).'
...Finish Email...';
$headers = '...Create Email Headers...';
if (mail($row->primary_email, $subject, $email_message, $headers)) {
$query = mysql_query("...log successful send...");
if (mysql_error()!="") {
$message .= '...display mysqlerror()..';
}
$message .= '...create success message...';
} else {
$query = mysql_query("...log failed send...");
if (mysql_error()!="") {
$message .= '...display mysqlerror()..';
}
$message .= '...create failed message...';
}
return $message;
} // END send_email() function
//Check supplier info
$query = mysql_query("...get suppliers info attached to order_id...");
if (mysql_num_rows($query) > 0) {
while($row = mysql_fetch_object($query)) {
if ($row->primary_email=="") {
$message .= '...no email message...';
} else if ($row->notification_email=="") {
$message .= '...no notifications message...';
} else {
$message .= send_email($order_id);
}
}
} else {
$message .= '...no supplier matched message...';
}
print $message;
?>
推荐答案
创建一个函数并将其包括在内
执行分离功能.来自邮件发送(不需要)的身份验证(需要会话)
然后在两个任务中都包含邮件发送功能.
make a function and include it
Do separate functions. Authentication (which requires sessions) from mail sending (which don't)
Then include mail sending function into both tasks.
这篇关于编写对php的调用的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!