繁体   English   中英

如何在PHP文件中添加CURL以将表单详细信息发送到电子邮件和外部数据

[英]How to add CURL in php file to send form details both to email and to external data

我想知道如何将表单详细信息发送到外部URL以及电子邮件ID。 我在编程发送到电子邮件ID的表单详细信息方面有经验,但是我的一位客户要求将表单详细信息的副本重定向到url,例如http://someipaddress.com/XDKRT/SalLeadEntWeb.ASP ,网站是由wordpress开发的,有人可以指导我如何实现吗?,我知道使用CURL可以实现这一目标,但是可以在哪里添加CURL? 我的PHP操作表是这样的:

<?php ob_start(); ?>
<?php
$contact_name = $_POST['name'];
$contact_email = $_POST['email'];
$contact_phone = $_POST['phone'];
$contact_message = $_POST['message'];

if( $contact_name == true )
{
    $sender = $contact_email;

    $receiver  = 'info@compositedge.com' . ',referral@compositeinvestments.com'; // note the comma

    $client_ip = $_SERVER['REMOTE_ADDR'];

    $email_body = "Name: $contact_name \nEmail: $contact_email \nPhone No: $contact_phone \nMessage: $contact_message \n";  

    $extra = "From: info@compositedge.com\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();

    if( mail( $receiver, "Open an Account - Download and Print", $email_body, $extra ) ) 
    {
    //IF SUCCESSFUL, REDIRECT
header("Location: http://www.mydomain.com/?page_id=1112");
    }
    else
    {
        echo "success=no";
    }
}
?>
<?php ob_flush(); ?>

要求帮助我,在哪里添加CURL?

这是在调用mail()之前添加了cURL请求的代码。

<?php ob_start();

$contact_name = $_POST['name'];
$contact_email = $_POST['email'];
$contact_phone = $_POST['phone'];
$contact_message = $_POST['message'];

if( !empty($contact_name)) {
    $sender = $contact_email;
    $receiver  = 'info@compositedge.com' . ',referral@compositeinvestments.com'; // note the comma
    $client_ip = $_SERVER['REMOTE_ADDR'];
    $email_body = "Name: $contact_name \nEmail: $contact_email \nPhone No: $contact_phone \nMessage: $contact_message \n";  
    $extra = "From: info@compositedge.com\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();

    // URL for cURL to post to
    $url        = 'http://someipaddress.com/XDKRT/SalLeadEntWeb.ASP';

    // Postfields for cURL to send
    // NOTE: You probably need to change the array keys to what the remote server expects to receive
    $postFields = array('name' => $contact_name,
                        'email' => $contact_email,
                        'phone' => $contact_phone,
                        'message' => $contact_message);

    // initialize curl and options
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);

    // send curl request
    $res = curl_exec($ch);
    curl_close($ch);
    // you can examine $res here to see if the request was successful


    if( mail( $receiver, "Open an Account - Download and Print", $email_body, $extra ) ) {
        // IF SUCCESSFUL, REDIRECT
        header("Location: http://www.mydomain.com/?page_id=1112");
    } else {
        echo "success=no";
    }
}

ob_flush();

除了$url ,cURL可能唯一需要更改的是$postFields的名称。 您应该根据远程URL期望发送的内容进行更改。

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM