【问题标题】:cURL with HTML content带有 HTML 内容的 cURL
【发布时间】:2013-08-27 08:20:38
【问题描述】:

我需要发布到一个 URL,我正在使用 curl 执行此操作。但问题在于我发布的 HTML 内容。我正在使用我要求发送 html 电子邮件的此页面。所以它将具有内联样式。当我 urlencode() 或 rawurlenocde() 时,这些样式属性被剥离。所以邮件看起来不正确。我怎样才能避免这种情况并按原样发布 HTML?

这是我的代码:

            $mail_url  = "to=".$email->uEmail;
            $mail_url .= "&from=info@domain.com";
            $mail_url .= "&subject=".$email_campaign[0]->email_subject;
            $mail_url .= "&type=signleOffer";
            $mail_url .= "&html=".rawurlencode($email_campaign[0]->email_content);

            //open curl request to send the mail
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_POST,count(5));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$mail_url);
            //execute post
            $result = curl_exec($ch);

【问题讨论】:

  • 如果您正确使用 cURL POST,则不需要使用urlencode()rawurlenocde(),请出示您的代码。
  • 与@l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇƇhƐȓ0nè 一起使用,您不需要对其进行编码。但是,即使您确实决定对其进行编码,也只需使用 decode 函数将 html 恢复为原始状态(例如 rawurldecode)
  • 我现在已经添加了代码。但如果我不使用这些功能,我就会失去风格。我认为它在 POST 期间被剥夺了安全性或其他原因。如果我不使用这些,我会收到一封空邮件,因为页面正在使用 rawurldecode

标签: php post curl urlencode


【解决方案1】:

这是一个示例,使用 http_build_query() 从值数组构建您的帖子数据:

<?php 
//Receiver debug
if($_SERVER['REQUEST_METHOD']=='POST'){
    file_put_contents('test.POST.values.txt',print_r($_POST,true));
    /*
    Array
    (
        [to] => example@example.com
        [from] => info@domain.com
        [subject] => subject
        [type] => signleOffer
        [html] => 
        <!DOCTYPE HTML>
        <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Mail Template</title>
            <style>.yada{color:green;}</style>
        </head>

        <body>
            <p style="color:red">Red</p>
            <p class="yada">Green</p>
        </body>
        </html>

    )
    */
    die;
}

$curl_to_post_parameters = array(
    'to'=>'example@example.com',
    'from'=>'info@domain.com',
    'subject'=>'subject',
    'type'=>'signleOffer',
    'html'=>'
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Mail Template</title>
        <style>.yada{color:green;}</style>
    </head>

    <body>
        <p style="color:red">Red</p>
        <p class="yada">Green</p>
    </body>
    </html>
    '
);

$curl_options = array(
    CURLOPT_URL => "http://localhost/test.php",
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $curl_to_post_parameters ), //<<<
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
);

$curl = curl_init();
curl_setopt_array($curl, $curl_options);
$result = curl_exec($curl);

curl_close($curl);
?>

【讨论】:

    【解决方案2】:

    按照本文所述执行POSTPassing $_POST values with cURL

    应该可以解决你的问题。

    【讨论】:

    • 它没有告诉任何关于 HTML 的内容
    • Alwin,如果您将 HTML 内容作为参数传递给 PHP CURL 中的“post fields”,则无需显式转义。这在帖子中很明显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多