Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这两个PHP代码片段合并在一起,以发送电子邮件和将XML发送到数据库?_Php_Xml_Email - Fatal编程技术网

如何将这两个PHP代码片段合并在一起,以发送电子邮件和将XML发送到数据库?

如何将这两个PHP代码片段合并在一起,以发送电子邮件和将XML发送到数据库?,php,xml,email,Php,Xml,Email,我通过网站表单生成潜在客户。旧系统使用以下代码通过电子邮件发送潜在客户: <?php if(isset($_POST['submit'])) { $to = "example@example.com"; $subject = "Email Subject"; // data the visitor provided $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING); $first_name = filter_var(

我通过网站表单生成潜在客户。旧系统使用以下代码通过电子邮件发送潜在客户:

<?php
if(isset($_POST['submit'])) {
$to = "example@example.com";
$subject = "Email Subject";

// data the visitor provided
$title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
$first_name = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$when_to_call = filter_var($_POST['whentocall'], FILTER_SANITIZE_STRING);
$house_number = filter_var($_POST['housenumber'], FILTER_SANITIZE_STRING);
$postcode = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);
$kw = filter_var($_POST['kw'], FILTER_SANITIZE_STRING);
$pos = filter_var($_POST['pos'], FILTER_SANITIZE_STRING);
$device = filter_var($_POST['device'], FILTER_SANITIZE_STRING);
$adgroup = filter_var($_POST['adgroup'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " From: $title $first_name $last_name\n\n Email: $email\n\n Phone: $phone\n\n When to call: $when_to_call\n\n House Name/Number: $house_number\n\n Postcode: $postcode \n\n Keyword: $kw \n\n Position: $pos \n\n Device: $device \n\n ad group: $adgroup";

// ...and away we go!
mail($to, $subject, $body);

// redirect to confirmation
header('Location: callbackconfirm.php');
} else {

}
?>

我们的新系统是通过XML将潜在客户直接发送到在线CRM中,下面是我的代码,它可以正常工作:

<?php
echo "Thank you for your submission ".$_POST["firstname"]; 

$XML = "";
$XML .= "<Application><Cases><Case>";

$XML .= "<CreateCase>1</CreateCase>";

$XML .= "<FirstName>".$_POST["firstname"]."</FirstName>";

$XML .= "<LastName>".$_POST["surname"]."</LastName>";

$XML .= "<HomeTelephone>".$_POST["phone"]."</HomeTelephone>";

$XML .= "<HouseNumber>".$_POST["housenumber"]."</HouseNumber>";

$XML .= "<PostCode>".$_POST["postcode"]."</PostCode>";

$XML .= "<KEYWORD>".$_POST["kw"]."</KEYWORD>";

$XML .= "<ADGROUP>".$_POST["adgroup"]."</ADGROUP>";

$XML .= "<DEVICE>".$_POST["device"]."</DEVICE>";

$XML .= "<POSITION>".$_POST["pos"]."</POSITION>";

$XML .= "<URL>".$_POST["currentUrl"]."</URL>";

$XML .= "<IPADDRESS>".$_POST["ipaddress"]."</IPADDRESS>";

$XML .= "<SourceName>".$_POST["adgroup"]."</SourceName>";

$XML .= "</Case></Cases></Application>";

postXML($XML);



function postXML($XML){
$url = "CRM URL";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"
);

$post = http_build_query(
    array("XMLApplication" => $XML)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);
}

?>

我需要的是能够将这两者合并在一起,这样当按下神奇的提交按钮时,它会将这个XML提要发送到软件中,同时生成电子邮件

我尝试过但失败了,有人能帮我吗


非常感谢

这有什么原因不起作用吗

PHP文件

<?php
if(isset($_POST['submit'])) {
    $to = "example@example.com";
    $subject = "Email Subject";

    // data the visitor provided
    $title          = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
    $first_name     = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
    $last_name      = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
    $email          = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
    $phone          = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    $when_to_call   = filter_var($_POST['whentocall'], FILTER_SANITIZE_STRING);
    $house_number   = filter_var($_POST['housenumber'], FILTER_SANITIZE_STRING);
    $postcode       = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);
    $kw             = filter_var($_POST['kw'], FILTER_SANITIZE_STRING);
    $pos            = filter_var($_POST['pos'], FILTER_SANITIZE_STRING);
    $device         = filter_var($_POST['device'], FILTER_SANITIZE_STRING);
    $adgroup        = filter_var($_POST['adgroup'], FILTER_SANITIZE_STRING);

    //constructing the message
    $body = " From: $title $first_name $last_name\n\n Email: $email\n\n Phone: $phone\n\n When to call: $when_to_call\n\n House Name/Number: $house_number\n\n Postcode: $postcode \n\n Keyword: $kw \n\n Position: $pos \n\n Device: $device \n\n ad group: $adgroup";

    // ...and away we go!
    mail($to, $subject, $body);

    //now the XML part...   
    echo "Thank you for your submission $first_name";

    $XML = "";
    $XML .= "<Application><Cases><Case>";
    $XML .= "<CreateCase>1</CreateCase>";
    $XML .= "<FirstName>$first_name</FirstName>";
    $XML .= "<LastName>$last_name</LastName>";
    $XML .= "<HomeTelephone>$phone</HomeTelephone>";
    $XML .= "<HouseNumber>$house_number</HouseNumber>";
    $XML .= "<PostCode>$postcode</PostCode>";
    $XML .= "<KEYWORD>$kw</KEYWORD>";
    $XML .= "<ADGROUP>$adgroup</ADGROUP>";
    $XML .= "<DEVICE>$device</DEVICE>";
    $XML .= "<POSITION>$pos</POSITION>";
    $XML .= "<URL>".$_POST["currentUrl"]."</URL>";
    $XML .= "<IPADDRESS>".$_POST["ipaddress"]."</IPADDRESS>";
    $XML .= "<SourceName>".$_POST["adgroup"]."</SourceName>";
    $XML .= "</Case></Cases></Application>";

    postXML($XML);

    // redirect to confirmation
    header('Location: callbackconfirm.php');

} else {

}


function postXML($XML){
    $url = "CRM URL";

    $headers = array(
        "Content-Type: application/x-www-form-urlencoded"
    );

    $post = http_build_query(
        array("XMLApplication" => $XML)
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    curl_close($ch);
}

?>


编辑:将重定向放在XML部分之后

像这样合并代码并删除XML回显

<?php
if(isset($_POST['submit'])) {
$to = "example@example.com";
$subject = "Email Subject";

// data the visitor provided
$title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
$first_name = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$when_to_call = filter_var($_POST['whentocall'], FILTER_SANITIZE_STRING);
$house_number = filter_var($_POST['housenumber'], FILTER_SANITIZE_STRING);
$postcode = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);
$kw = filter_var($_POST['kw'], FILTER_SANITIZE_STRING);
$pos = filter_var($_POST['pos'], FILTER_SANITIZE_STRING);
$device = filter_var($_POST['device'], FILTER_SANITIZE_STRING);
$adgroup = filter_var($_POST['adgroup'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " From: $title $first_name $last_name\n\n Email: $email\n\n Phone: $phone\n\n When to call: $when_to_call\n\n House Name/Number: $house_number\n\n Postcode: $postcode \n\n Keyword: $kw \n\n Position: $pos \n\n Device: $device \n\n ad group: $adgroup";

// ...and away we go!
mail($to, $subject, $body);

$XML = "";

$XML .= "<Application><Cases><Case>";

$XML .= "<CreateCase>1</CreateCase>";

$XML .= "<FirstName>".$_POST["firstname"]."</FirstName>";

$XML .= "<LastName>".$_POST["surname"]."</LastName>";

$XML .= "<HomeTelephone>".$_POST["phone"]."</HomeTelephone>";

$XML .= "<HouseNumber>".$_POST["housenumber"]."</HouseNumber>";

$XML .= "<PostCode>".$_POST["postcode"]."</PostCode>";

$XML .= "<KEYWORD>".$_POST["kw"]."</KEYWORD>";

$XML .= "<ADGROUP>".$_POST["adgroup"]."</ADGROUP>";

$XML .= "<DEVICE>".$_POST["device"]."</DEVICE>";

$XML .= "<POSITION>".$_POST["pos"]."</POSITION>";

$XML .= "<URL>".$_POST["currentUrl"]."</URL>";

$XML .= "<IPADDRESS>".$_POST["ipaddress"]."</IPADDRESS>";

$XML .= "<SourceName>".$_POST["adgroup"]."</SourceName>";

$XML .= "</Case></Cases></Application>";   


function postXML($XML){
$url = "CRM URL";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"
);

$post = http_build_query(
    array("XMLApplication" => $XML)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);

postXML($XML);

// redirect to confirmation
header('Location: callbackconfirm.php');
} else {

}
?>


感谢您的回复。你是对的,变数到处都是

这就是最终为我工作的原因:

<?php
if(isset($_POST['submit'])) {
$to = "";
$subject = "Email Subject";

// data the visitor provided
$first_name     = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$surname        = filter_var($_POST['surname'], FILTER_SANITIZE_STRING);
$phone          = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$housenumber    = filter_var($_POST['housenumber'], FILTER_SANITIZE_STRING);
$postcode       = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);
$kw             = filter_var($_POST['kw'], FILTER_SANITIZE_STRING);
$pos            = filter_var($_POST['pos'], FILTER_SANITIZE_STRING);
$device         = filter_var($_POST['device'], FILTER_SANITIZE_STRING);
$adgroup        = filter_var($_POST['adgroup'], FILTER_SANITIZE_STRING);
$currentUrl     = filter_var($_POST['currentUrl'], FILTER_SANITIZE_STRING);
$ipaddress      = filter_var($_POST['ipaddress'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " From: $first_name $surname\n\n Phone: $phone\n\n  House Name/Number: $housenumber\n\n Postcode: $postcode \n\n Keyword: $kw \n\n Position: $pos \n\n Device: $device \n\n ad group: $adgroup \n\n URL: $currentUrl \n\n IP Address: $ipaddress";

// ...and away we go!
mail($to, $subject, $body);

//now the XML part...   


$XML = "";
$XML .= "<Application><Cases><Case>";
$XML .= "<CreateCase>1</CreateCase>";
$XML .= "<FirstName>$first_name</FirstName>";
$XML .= "<LastName>$surname</LastName>";
$XML .= "<HomeTelephone>$phone</HomeTelephone>";
$XML .= "<HouseNumber>$housenumber</HouseNumber>";
$XML .= "<PostCode>$postcode</PostCode>";
$XML .= "<KEYWORD>$kw</KEYWORD>";
$XML .= "<ADGROUP>$adgroup</ADGROUP>";
$XML .= "<DEVICE>$device</DEVICE>";
$XML .= "<POSITION>$pos</POSITION>";
$XML .= "<URL>".$_POST["currentUrl"]."</URL>";
$XML .= "<IPADDRESS>".$_POST["ipaddress"]."</IPADDRESS>";
$XML .= "<SourceName>".$_POST["adgroup"]."</SourceName>";
$XML .= "</Case></Cases></Application>";

postXML($XML);

// redirect to confirmation
header('Location: callbackconfirm.php');

} else {

}


function postXML($XML){
$url = "";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"
);

$post = http_build_query(
    array("XMLApplication" => $XML)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);
}

?>


您需要更具体地说明您遇到的问题。这似乎太明显了,只需在
mail()
header()函数调用之间复制XML代码即可。您可能需要删除
回音“谢谢…”
对,那么到目前为止您做了哪些尝试?最好将xml代码放在“header”('Location:callbackconfirm.php');“是的,我已经纠正了这一点。还有一个差异-OP有
$\u POST['names']
$\u POST['last\u name']
,在XML部分,
$\u POST['adgroup']
被引用了两次。是的,基本上OP是在问显而易见的问题,因为他们不是程序员,希望我们中的一个人为他们做所有的工作,当然是免费的。这取决于你对是否也为他们解决了所有稍微不明显的问题的慷慨程度。嗯,我会留下评论提醒他们注意这些问题,但我在等待在我为完成的工作开具发票之前,我正在等待他们测试代码……哈哈。祝你好运。非常感谢这一点,因为我的问题,我不得不稍微编辑一下,但这正是让我走上成功之路的原因!