首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在windows azure中使用cURL调用php

在windows azure中使用cURL调用php
EN

Stack Overflow用户
提问于 2015-01-02 22:27:32
回答 1查看 3.1K关注 0票数 6

当前,我在尝试将Azure用作SMTP服务器时遇到问题。我正在尝试创建一个简单的联系人表单,将发送电子邮件时,您点击发送。PHP代码很简单,可以在另一台服务器上运行,因为它来自以前的项目,但我现在需要使用Microsoft Azure服务器,从我读到的内容来看,我需要使用cURL或sendmail API调用。有没有人知道怎么做,因为我好像不能让它工作。这是微软说你需要用来让cURL正常工作的代码,

代码语言:javascript
复制
// 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代码,如果它有帮助的话

代码语言:javascript
复制
$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>';
    }
}
    }
EN

回答 1

Stack Overflow用户

发布于 2015-02-05 23:13:58

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

代码语言:javascript
复制
   $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停止响应设计布局有关的其他问题(如果发生这种情况,请检查你的页面源代码,看看你的页面源代码中是否有链接,点击链接,看看这是否解决了你的响应问题),如果有人对上面的代码有任何问题,请随时给我发邮件,我通常会在一天内给你回复。

索克斯

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27743701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档