-2

This is my php code. I need to send here 3 parameters like cammand , mobile and token. include "../db.php";

//$_POST['command'] ;//
$command = $_POST['command'];
if ($command == "register_user") {//register user
    $mobile = $_POST['mobile'];
    $token = $_POST['token'];
    $sql = "SELECT * FROM  tbl_user where mobile ={$mobile}";
    $result = mysqli_query($connection, $sql);
    $num = mysqli_num_rows($result);

This my swift code.

import Foundation
import UIKit
import Alamofire
class LoginViewController: UIViewController {
    
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
     
    @IBOutlet weak var inputTelephoneNumber: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
    }
    
    @IBAction func loginBtn(_ sender: Any) {
        
        let Parametrs : Parameters = [
            
            "command": "register_user",
            "mobile": inputTelephoneNumber.text!,
            "token": appDelegate.myToken!
            
        ]
        AF.request("http://192.xxxxxxxxx/xxxxxxxx/api/sms_verify.php", method: .post, parameters: Parametrs, encoding: JSONEncoding.default)
        .responseJSON { response in
            print(response)
    } 
        
       let tokenURL = "http://192xxxxxx/xxxxxxxxx/api/sms_verify.php"
       if let url = URL(string: tokenURL) {
           let task = URLSession.shared.dataTask(with: url) {
               data, response, error in
               if error != nil {
                   print(error!)
               } else {
                   if let responseString = String(data: data!, encoding: .utf8) {
                       print(responseString)
                   }
               }
           }
           task.resume()
       }
   
    }
    
    
   


}

I get this error from xcode.

<br />
<b>Notice</b>:  Undefined index: command in <b>/opt/lampp/htdocs/foroshgah1/api/sms_verify.php</b> on line <b>5</b><br />

failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
  1. I try to send three parameters like cammand = register_user , mobile and token.

  2. I try to send them like json encode but I did not know I correct form or no?

Alireza
  • 1
  • 1
  • 1
    Does this answer your question? [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – 04FS Sep 29 '20 at 07:45
  • My Php code work fine without any problem. I have problem in swift – Alireza Sep 29 '20 at 07:50
  • If you are sending your data as JSON, then you need to modify your PHP code - PHP _does not_ populate $_POST, when you send JSON. Or you need to send something else from the client side in the first place - like a “normal” `application/x-www-form-urlencoded` request. – 04FS Sep 29 '20 at 07:56
  • I say that my Php code work fine and my android app work with this Php very fine. – Alireza Sep 29 '20 at 07:59
  • If you have experience in Alamofire write comment. – Alireza Sep 29 '20 at 08:00
  • You aren't sending an array, you are sending a dictionary. You have at the same time a URLSession code and a Alamofire one, it's unclear. If you code works well with Android, might want to share the code? Does it work with a `cURL` command too? POSTMAN? – Larme Sep 29 '20 at 08:13
  • Can you send correct format code? – Alireza Sep 29 '20 at 08:22
  • @Alireza just change the `encoding` that you send to request function from `JSONEncoding.default` to `URLEncoding.default`. – gcharita Sep 29 '20 at 08:32
  • @gcharita, please post your comment as an answer. Alireza please accept the answer that gcarita posts. – jnovack Sep 29 '20 at 10:37
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 29 '20 at 10:43

1 Answers1

0

To pass URL encoded parameters in a post request, you need to call the request function of Alamofire passing an instance of URLEncoding as encoding parameter:

AF.request(
    "http://192.xxxxxxxxx/xxxxxxxx/api/sms_verify.php",
    method: .post,
    parameters: Parametrs,
    encoding: URLEncoding.default
).responseJSON { response in
    print(response)
}
gcharita
  • 7,729
  • 3
  • 20
  • 37