【问题标题】:php call with cURL in windows azure在 Windows azure 中使用 cURL 进行 php 调用
【发布时间】:2015-04-11 13:41:56
【问题描述】:

目前我在尝试将 Azure 用作 SMTP 服务器时遇到问题。我正在尝试创建一个简单的联系表格,当您点击发送时会发送一封电子邮件。 PHP 代码很简单,并且可以在另一个服务器上运行,因为它来自以前的项目,但是我现在需要使用 Microsoft Azure 服务器,从我阅读的内容来看,我需要使用 cURL 或 sendmail API 调用。有谁知道如何做到这一点,因为我似乎无法让它工作。这是微软说你需要用来让 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 代码中的什么地方才能让它工作呢?在天蓝色的一面,我还缺少什么吗?我已经在我的帐户上安装了 sendmail,这正是他们所说的我所需要的。

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

$url = 'https://api.sendgrid.com/';
 $user = 'azure_0386579c9@azure.com';
 $pass = 'password7'; 

 $params = array(
      'api_user' => $user,
      'api_key' => $pass,
      'to' => 'hidden@gmail.com',
      'subject' => 'testing from curl',
      'html' => 'testing body1',
      'text' => 'testing body2',
      'from' => 'hidden@gmail.com',
   );

 $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 = 'hidden@gmail.com'; 
        $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>';
    }
}
    }

【问题讨论】:

  • 在你的curl_exec 之后添加这个以获得一些额外的调试信息:if (curl_errno($session)) { echo 'Curl error: ' . curl_error($session); }。将其添加到您的问题中,看看我们是否可以解决此问题。来自php.net/manual/en/function.curl-errno.php
  • 感谢您的帮助,Tom,实际上我在本周开始时就设法让它工作了。把帖子的答案放在那里解释我的工作(和错误)感谢你的帮助:)

标签: php azure curl


【解决方案1】:

谁想看到一些糟糕的编码实践???因此,经过大量的拉扯和研究,我找到了一种让我的 PHP 表单工作的方法。我将使用自我解释变量编辑代码,以便通读代码,并希望它应该清楚为什么事情在某些地方。请记住,这仅在您拥有 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' => 'thisiswhereitsgoingto@gmail.com',
      '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 = 'thisiswhereitsgoing@gmail.com'; 
        $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'        => 'whereEmailIsGoing@gmail.com',
                    '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 'mail' 功能工作的不同服务器,这更容易,我还发现 Windows azure 的其他问题与 iFrames 停止响应式设计布局有关(如果发生这种情况,请检查您的页面源,看看您的页面源中是否有链接,点击链接,看看是否能解决您的响应问题)如果有人对上面的代码有任何疑问,请随时给我发电子邮件'通常会在一天内回复您。

敏捷

【讨论】:

  • 您好,两天后,我找到了您的帖子!现在只需稍微修改您的代码 - 最终解决了 Azure PHP 的巨大谜团。没有你,我的生活很悲惨。非常感谢!
猜你喜欢
  • 2013-07-05
  • 2013-06-12
  • 1970-01-01
  • 2015-01-02
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
相关资源
最近更新 更多