2017-04-10 33 views
0

我有Xcode中8的Xcode 8雨燕3.0:解析錯誤 - 無法讀取PHPMailer的代碼

問題在PHP中,我使用的PHPMailer發送電子郵件。我的PHP代碼如下。

send.php

<?php 
    require 'database/connect.php'; 
    global $connect; 
    date_default_timezone_set('Etc/UTC'); 
    require 'PHPMailer-master2/PHPMailerAutoload.php'; 

    if (isset($_POST['data1']) && isset($_POST['data2'])) 
    { 
     $data1 = $_POST['data1']; 
     $data2 = $_POST['data2']; 

     $sql  = "SELECT * FROM table WHERE data1 = '$data1' AND data2='$data2'"; 
     $result  = mysqli_query($connect, $sql); 
     if ($result && mysqli_num_rows($result) > 0) 
     { 
      while ($row = mysqli_fetch_array($result)){ 
      } 

      $output = array('message' => '1'); 
      echo json_encode($output); 
      $add = "INSERT INTO table (data1, data2) 
           VALUES ('$data1','$data2') 
      "; 
      $run = mysqli_query($connect,$add); 
      $mail = new PHPMailer;        
      $mail->isSMTP();          
      $mail->Host = 'smtp.gmail.com'; 
      $mail->SMTPAuth = true;         
      $mail->Username = 'gmail.com';    
      $mail->Password = '******';        
      $mail->SMTPSecure = 'tls';        
      $mail->Port = 587;          
      $mail->setFrom('[email protected]', 'sender'); 
      $mail->addAddress('[email protected]','receiver'); 
      $mail->isHTML(true);         
      $mail->Subject = 'Test'; 
      $mail->Body = 'Test'; 
      $mail->AltBody = 'Test'; 
     if(!$mail->send()) { 
      echo json_encode([ 
       'status' => false, 
       'message' => 'Message could not be sent. Error: ' . $mail->ErrorInfo 
      ]); 
     } else { 
      $status = array(); 
      $status[] = array('status' => '1'); 
     } 

     $output = array('message' => '1', 'status' => $status); 
     echo json_encode($output); 
     exit(); 
      // End sending email 
      exit(); 

     mysqli_free_result($result); 
     } 
     else {} 
    } 
?> 

我設法將數據發送到服務器,發送電子郵件使用上面的代碼到接收器。

我現在面臨的唯一問題是xcode。它說:

Parse error: The data couldn’t be read because it isn’t in the correct format.

enter image description here

Xcode中無法讀取PHP文件我PHPMailer的代碼,導致我的SWIFT 3.0代碼來執行,而不是消息的聲明== '1'聲明。我的swift代碼如下。

post.swift

@IBAction func sendApplyMovement(_ sender: Any) { 
      let url    = URL(string: "http://localhost/send.php") 
      let session   = URLSession.shared 
      let request   = NSMutableURLRequest(url: url! as URL) 
      request.httpMethod = "POST" 
      let valueToSend  = "data1=&data2" 
      request.httpBody = valueToSend.data(using: String.Encoding.utf8) 

      let myAlert   = UIAlertController(title: "Confirm", message: "Sure ?", preferredStyle: UIAlertControllerStyle.alert) 
      let cancel   = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil) 
      let okaction  = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: 
       { 
        action in 

      let task = session.dataTask(with: request as URLRequest, completionHandler: { 
       (data, response, error) in 
       if error != nil { 
        return 
       } 
       else { 
          do { 
           if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] 
           { 
            DispatchQueue.main.async { 
             let message = Int(json["message"]!) 
             let status = Int(json["status"]!) 

             if(message == 1){ 
              if(status == 1){ 
               print("Success") 
               let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController 
               let appDelegate = UIApplication.shared.delegate as! AppDelegate 
               let navigationController = UINavigationController.init(rootViewController: myViewController) 
               appDelegate.window?.rootViewController = navigationController 
               appDelegate.window?.makeKeyAndVisible() 

               let myAlert = UIAlertController(title: "Success!", message: "Sent !", preferredStyle: UIAlertControllerStyle.alert) 
               myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)) 
               navigationController.present(myAlert, animated: true, completion: nil) 
               return 
              } 

             } 
             else {return} 
            } 
           } 
          } 
          catch let parseError { print("Parse error: \(parseError.localizedDescription)") } 
       } 
      }) 
      task.resume() 

      } 
      ) 
      myAlert.addAction(okaction) 
      myAlert.addAction(cancel) 
      self.present(myAlert, animated: true, completion: nil) 
     } 
    } 

有,我需要以使它工作要修改的東西嗎?

回答

1

你這樣做:

if let json = try JSONSerialization.jsonObject(with: data!) 

這意味着你得到的數據是JSON格式,但你的PHPMailer的代碼做這個:

if(!$mail->send()) 
{ 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} 
else 
{ 
    echo 'Message has been sent'; 
} 

沒有返回JSON代碼,所以我並不感到驚訝,因爲解析它時遇到了麻煩。你以前發佈過這個問題,但它很不明確 - 你聽起來像Xcode無法打開你的PHP文件,並不是說你無法解析響應;這是一個Swift運行時錯誤,而不是Xcode錯誤。

以JSON格式返回你的迴應,你可能會獲得更大的成功,是這樣的:

​​
+0

確定。我會先嚐試一下。 。 。 – AlotJai

+0

錯誤「分析錯誤」消失了,但仍然無法在消息內執行UIAlert == 1 – AlotJai

+0

只需閱讀代碼 - 它正在'message'元素內尋找值'1',但我在其中插入了一個字符串 - 你只是需要確保你輸出和輸入行 - 嘗試改變你的代碼爲'if(json [「status」])',因此它會查看'status'元素。 – Synchro