在 Windows Azure 中使用 cURL 进行 php 调用

标签 php azure curl

目前,我在尝试使用 Azure 作为 SMTP 服务器时遇到问题。我正在尝试创建一个简单的联系表单,当您点击“发送”时,该表单将发送电子邮件。 PHP 代码很简单,可以在另一台服务器上运行,因为它来自以前的项目,但我现在需要使用 Microsoft Azure 服务器,根据我的阅读,我需要使用 cURL 或 sendmail API 调用。有谁知道如何做到这一点,因为我似乎无法让它工作。这是 Microsoft 所说的让 cURL 正常工作所需的代码,

// Generate curl request
 $session = curl_init($request);

 // Tell curl to use HTTP POST
 curl_setopt ($session, CURLOPT_POST, true);

 // Tell curl that this is the body of the POST
 curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

// Tell curl not to return headers, but do return the response
 curl_setopt($session, CURLOPT_HEADER, false);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

 // obtain response
 $response = curl_exec($session);
 curl_close($session);

 // print everything out
 print_r($response);

我想这比我看到的要简单得多,但是我到底应该把我的 php 代码放在这个 cURL 代码中的哪里才能让它工作呢?在 azure 的一面上我还缺少什么吗?我的帐户上已经安装了 sendmail,这就是他们所说的我所需要的。

这里是我的 php 代码,如果有帮助的话

$url = 'https://api.sendgrid.com/';
 $user = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5849f909780bad5d6ddd3d0d2dc86dca5849f909780cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>';
 $pass = 'password7'; 

 $params = array(
      'api_user' => $user,
      'api_key' => $pass,
      'to' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076f6e6363626947606a666e6b2964686a" rel="noreferrer noopener nofollow">[email protected]</a>',
      'subject' => 'testing from curl',
      'html' => 'testing body1',
      'text' => 'testing body2',
      'from' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2aaaba6a6a7ac82a5afa3abaeeca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>',
   );

 $request = $url.'api/mail.send.json';

 if ($_POST["submit"]) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Contact Form'; 
        $to = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="442c2d2020212a042329252d286a272b29" rel="noreferrer noopener nofollow">[email protected]</a>'; 
        $subject = 'Message from Contact Form ';

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }

// If there are no errors, send the email
if (!$errName || !$errEmail || !$errMessage || !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }

最佳答案

谁想看到一些糟糕的编码实践???因此,经过大量的努力和研究,我找到了一种让我的 PHP 表单正常工作的方法。我将使用 self 解释变量来编辑代码,因此请通读代码,希望它应该清楚为什么事情在某些地方。请记住,只有当您有 Windows Azure 服务器并且出于某种原因需要 php 表单才能在服务器上工作时,这才有用。您需要在 Windows 门户上安装 sendmail,然后按照以下步骤获取 url、密码和用户名。这一切都像这样进入你的 php 文件。 (好吧,我的可以工作,但是里面有一些多余的代码,没有什么危险,只是我说我放弃了,它有效,我稍后会重新编写它)

   $url = 'https://api.sendgrid.com/';
 $user = 'this is provided user attribute';
 $pass = 'password1'; 

 $params = array(
      'api_user' => $user,
      'api_key' => $pass,
      'to' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9ddc1c0dac0dadec1ccdbccc0dddacec6c0c7ceddc6e9cec4c8c0c587cac6c4" rel="noreferrer noopener nofollow">[email protected]</a>',
      'subject' => 'subject of the email',
      'html' => 'I am the HTML parameter',
      'text' => 'I am the text parameter',
      'from' => $email,
   );

 $request = $url.'api/mail.send.json';

 if ($_POST["submit"]) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = "From: Contact Form";
        $mobile = $_POST['number'];

        $to = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36425e5f455f45415e5344535f424551595f585176515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>'; 
        $subject = 'Message for subject line of email';

        $humanBool=66;

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        // now we go through some validation of the parts in the form 
        // to check everything was entered. In hindsight HTML 5 
        // 'required' attribute is much easier and fulfills exactly 
        // what I did here anyway.
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }else{
          $humanBool = 66;
        }

        // If there are no errors in the data fields i.e. missing data
        if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
          //and the human anti spam field is correct.
            if($humanBool == 66){
              //do the email sending magic.
              //create url for api call
              // ready for that repetitive code?
                $url = 'https://api.sendgrid.com/';
                //create array params for api call
                //these params are what appear in the email that arrives into your email account.
                $params = array(
                    'api_user'  => $user,
                    'api_key'   => $pass,
                    'to'        => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="186f707d6a7d5d75797174516b5f7771767f587f75797174367b7775" rel="noreferrer noopener nofollow">[email protected]</a>',
                    'subject'   => 'Subject',
                    'html'      => "From: $name\n\r Message:\r\n $message",
                    'text'      => 'this is the text element',
                    'from'      => $email,
                  );

                // I don't why I concatenated this but one of the 
                // resources I used while researching said to do it. It 
                // worked, it's also unneccessary. $request is just 
                // https://api.sendgrid.com/api/mail.send.json. I think 
                // the founder of that article is just having a private 
                // joke at people using his code for help.

                //concatenate api url to url above
                $request =  $url.'api/mail.send.json';

                //debugging
                //$error = error_get_last();
                //echo this to see what errors are happening in the file

                // Repetitive code.....
                $url2 = 'https://api.sendgrid.com/api/mail.send.json';


                // Okay queue magic time. I'll explain it as overview
                // here and you guys can step through after the 
                // explanation. 1) magic. 2) Sorcery. I don't quite get 
                // all of it so my explanation would be poor but I
                // will say HOW it works overall. All previous arrays
                // and variables are packaged up in one pack and then
                // a service is called and they are sent as $result 



                // use key 'http' even if you send the request to https://
                $options = array(
                    'http' => array(
                        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                        'method'  => 'POST',
                        'content' => http_build_query($params),
                    ),
                );
                $context  = stream_context_create($options);
                $result = file_get_contents($url2, false, $context);

                // debugging code if something goes wrong 
                // var_dump($result);
                $result='<div class="alert alert-success">Thank You! I will be in touch</div>';

                // this is here to reset the page and clear the fields
                // of the form once mail has been sent.
                $page = $_SERVER['PHP_SELF'];
                $sec = "3";
                header("Refresh: $sec; url=$page");

            }else{
                  $result='<div class="alert alert-danger">Human checked failed</div>';
            }


          }else{
              $result='<div class="alert alert-danger">Validation error</div>';
          }
}


?>
// after this goes the HTML form here is one box from the form as its 
// all the same no need to repeat it all I think.

<div class="form-group">

                        <div class="col-xs-10 col-xs-offset-1">
                            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" style="text-transform:capitalize" value="<?php echo htmlspecialchars($_POST['name']); ?>" required>
                            <?php echo "<p class='text-danger'>$errName</p>";?>
                        </div>

现在我已经解决了这个问题,如果需要,请随意使用我的代码供您自己使用。我强烈建议你不要使用 windows azure 来做这类事情,而只是获取一个不同的服务器,其中 php 的“邮件”功能可以工作,这要容易得多,我还发现 windows azure 的其他问题与 iFrame 停止响应式设计布局有关(如果发生这种情况,请检查您的页面源,看看您的页面源中是否有链接,点击该链接,看看是否可以解决您的响应问题)一如既往,如果有人对上述代码有任何疑问,请随时给我发电子邮件通常会在一天内回复您。

德克斯

关于在 Windows Azure 中使用 cURL 进行 php 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743701/

相关文章:

php - 我有两个 if 和一个 else 条件,但 else 条件不起作用并且总是显示

php - 将表单数据从表单传输到外部表单

php - 使用Simple HTML Dom的错误PHP网站爬虫类

sql-server - 打开与多个数据库的连接 - 唤醒

php - file_get_contents() 的更快替代方案

php - 如何处理 php 生成的图像(不带扩展名)?

php - While 循环未编译(PHP V 5.3.8)

sql - 在 SQL Azure 中安全地更改列数据类型

azure - 更新 Terraform 中现有的 Azure 应用服务

php - Amazon MWS Api SubmitFeed _POST_FLAT_FILE_INVLOADER_DATA_ incorrect template type 错误