2015-01-02 6 views
6

में कर्ल के साथ PHP कॉल वर्तमान में मुझे एक एसएमटीपी सर्वर के रूप में Azure का उपयोग करने का प्रयास करने में समस्या आ रही है। मैं एक साधारण संपर्क फ़ॉर्म बनाने की कोशिश कर रहा हूं जो आपको भेजे जाने पर एक ईमेल भेजेगा। PHP कोड सरल है और किसी अन्य सर्वर पर काम करता है क्योंकि यह पिछले प्रोजेक्ट से है, लेकिन मुझे अब माइक्रोसॉफ्ट एज़ूर सर्वर का उपयोग करने की ज़रूरत है और जो मैंने पढ़ा है, उसे मुझे कर्ल या सेंडमेल एपीआई कॉल का उपयोग करने की ज़रूरत है। क्या कोई जानता है कि ऐसा कैसे करें क्योंकि मैं इसे काम करने के लिए प्रतीत नहीं कर सकता। इस कोड को माइक्रोसॉफ्ट का कहना है कि आप 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); 

मैं इस कल्पना बहुत सरल तो मैं देख सकता हूँ है, लेकिन पाने के लिए जहां वास्तव में मैं इस cURL कोड में मेरे php कोड रखूँ यह काम करने के लिए? क्या कुछ और चीज है जो मैं चीजों के अजीब पक्ष में भी लापता हूं? मेरे पास पहले से ही मेरे खाते पर sendmail स्थापित है जो मैंने कहा था कि मुझे चाहिए था। अगर यह मदद करता है

यहां किसी भी तरह अपना php कोड है

$url = 'https://api.sendgrid.com/'; 
$user = '[email protected]'; 
$pass = 'password7'; 

$params = array(
     'api_user' => $user, 
     'api_key' => $pass, 
     'to' => '[email protected]', 
     'subject' => 'testing from curl', 
     'html' => 'testing body1', 
     'text' => 'testing body2', 
     'from' => '[email protected]', 
    ); 

$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 = '[email protected]'; 
     $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>'; 
    } 
} 
    } 
+1

अपने 'curl_exec' के बाद इस जोड़े कुछ अतिरिक्त डिबग जानकारी पाने के लिए:' अगर (curl_errno ($ सत्र)) {गूंज 'कर्ल त्रुटि:'। curl_error ($ सत्र); } '। इसे अपने प्रश्न में जोड़ें और देखें कि क्या हम इस समस्या को हल कर सकते हैं। Http://php.net/manual/en/function.curl-errno.php – Tom

+0

आपकी मदद के लिए धन्यवाद टॉम, मैं वास्तव में सप्ताह की शुरुआत में इसे काम करने में कामयाब रहा। मेरी कार्यप्रणाली (और गलतियों) की व्याख्या करने के लिए वहां पोस्ट के लिए एक उत्तर दें, मदद करने के आपके प्रयास की सराहना करें :) –

उत्तर

5

कौन कुछ बुरी कोडन प्रथाओं को देखना चाहता है ??? तो, बहुत सारे बाल खींचने और शोध के बाद मैंने अपने PHP फॉर्म को काम करने के लिए एक रास्ता निकाला। मैं कोड को स्वयं व्याख्या करने वाले चर के साथ संपादित करूंगा ताकि कोड के माध्यम से पढ़ सकें और उम्मीद है कि यह स्पष्ट हो जाना चाहिए कि चीजें कुछ स्थानों पर क्यों हैं। याद रखें यह केवल तभी उपयोगी है जब आपके पास विंडोज़ एज़ूर सर्वर है और किसी कारण से सर्वर पर काम करने के लिए आपको एक PHP फॉर्म की आवश्यकता है। आपको अपने विंडोज पोर्टल पर sendmail इंस्टॉल करने की आवश्यकता है, फिर आप यूआरएल, पासवर्ड और उपयोगकर्ता नाम प्राप्त करने के लिए चरणों का पालन करें। यह सब आपकी PHP फ़ाइल में ऐसा ही जाता है। (ठीक है, मेरा काम करता है लेकिन वहाँ में, खतरनाक कुछ भी नहीं कोड की कुछ बेमानी बिट्स theres बस मुझे यह कहते हुए मैंने छोड़ दिया, यह काम करता है, मैं एक बाद की तारीख में यह rework होगा) अब मैं इस है

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

$params = array(
     'api_user' => $user, 
     'api_key' => $pass, 
     'to' => '[email protected]', 
     '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 = '[email protected]'; 
     $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'  => '[email protected]', 
        '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> 

काम करने के लिए कृपया अगर आप की जरूरत है तो अपने कोड के लिए अपना कोड लेने में संकोच न करें। मैं पूरी तरह से अनुशंसा करता हूं कि आप इस तरह की चीज़ों के लिए विंडोज़ एज़ूर का उपयोग न करें और केवल एक अलग सर्वर प्राप्त करें जहां php 'mail' फ़ंक्शन काम करता है जो बहुत आसान है, मुझे विंडोज़ एज़ूर के साथ अन्य समस्याएं भी मिलीं जिनमें उत्तरदायी डिज़ाइन लेआउट को रोकने वाले आईफ्रेम (यदि ऐसा होता है तो अपने पृष्ठ स्रोत की जांच करें और देखें कि क्या आपके पृष्ठ स्रोत में कोई लिंक है, लिंक का पालन करें और देखें कि क्या यह आपकी उत्तरदायी समस्या हल करता है) और हमेशा अगर किसी के ऊपर दिए गए कोड के बारे में कोई प्रश्न है तो कृपया मुझे ईमेल करने के लिए स्वतंत्र महसूस करें I आम तौर पर एक दिन के भीतर आप वापस आ जाएगा।

डेक्स

+2

हाय वहाँ, 2 दिनों के बाद, मुझे आपकी पोस्ट मिली है! अभी आपके कोड को थोड़ा सा संशोधित करें - और अंततः महान Azure PHP रहस्य हल किया। मेरा जीवन तुम्हारे बिना दुखी रहा है। आपका बहुत बहुत धन्यवाद! – ForeverLearning

+2

आपको बहुत बहुत धन्यवाद! –

संबंधित मुद्दे