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


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

我想知道如何将表单详细信息发送到外部url和电子邮件idhttp://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();

除了cURL的$url之外,您可能只需要更改$postFields的名称。您应该根据远程URL期望发送的内容来更改此设置。

希望能有所帮助。