用Windows中的cURL调用php

Dex*_*lan 6 php curl azure

目前我在尝试使用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);
Run Code Online (Sandbox Code Playgroud)

我想这比我看到的要简单得多,但是我在哪里把我的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>';
    }
}
    }
Run Code Online (Sandbox Code Playgroud)

Dex*_*lan 5

谁想看到一些不好的编码习惯??? 因此,经过大量的研究和研究,我找到了一种方法来让我的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>
Run Code Online (Sandbox Code Playgroud)

现在我已经解决了这个问题,如果需要,请随意将我的代码用于您自己的用途.我完全建议你不要使用windows azure这种东西,只是得到一个不同的服务器,其中php'邮件'功能工作,这更容易,我还发现其他问题与windows azure与iFrames停止响应设计布局(检查您的页面来源,如果发生这种情况,看看您的页面源中是否有链接,请点击链接,看看是否能解决您的响应问题)如果有人对上述代码有任何疑问,请随时给我发电子邮件我通常会在一天之内回复你.

地塞米松

  • 您好,2天后,我找到了您的帖子!稍微修改了你的代码 - 最终解决了伟大的Azure PHP之谜.没有你,我的生活一直很悲惨.非常感谢! (2认同)
  • 非常感谢! (2认同)