Q:

Alamofire JSON could not be serialized

I'm trying to make a PHP Post request with Alamofire in my swift app, which has to send SMS with OVH api, but I got an issue : 
FAILURE: Error Domain=com.alamofire.error Code=-6006 "JSON could not be serialized. Input data was nil or zero length." UserInfo={NSLocalizedFailureReason=JSON could not be serialized. Input data was nil or zero length.} 
I'm not finding how to see echos from the PHP script to solve it. I think that it isn't an Alamofire problem because I make an other request to send emails and it works well. Here is my request where there is the error : 
func sendSmsAutoRequest(completionHandler: (responseObject: NSDictionary?, error: NSError?) -> ()) {
    let prefs = NSUserDefaults.standardUserDefaults()
    let username = prefs.stringForKey(StorageManager.USER_NAME)!
    let messageText = prefs.stringForKey(StorageManager.MESSAGE_TEXT)!

    let parameters = ["contactsSms": contactsSms, "username": username, "messageText": messageText]
    request(.POST, "http://XXX/dontworry/sendAutoSms.php", parameters: (parameters as! [String : AnyObject])).responseJSON { response in
        print(response)
    }

}

and here is my script : 
<?php

require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;

$applicationKey = "XXX";
$applicationSecret = "XXX";
$consumer_key = "XXX";

$endpoint = "ovh-eu";

$contactsSms = $_POST['contactsSms'];
$userName = $_POST['username'];
$messageText = $_POST['messageText'];

$conn = new Api($applicationKey,
                $applicationSecret,
                $endpoint,
                $consumer_key);

$smsServices = $conn->get('/sms/');

$content = (object) array(
                            "charset"=> "UTF-8",
                            "class"=> "phoneDisplay",
                            "coding"=> "7bit",
                            "message"=> $messageText,
                            "noStopClause"=> true,
                            "priority"=> "high",
                            "receivers"=> $contactsSms,
                            "senderForResponse"=> true,
                            "validityPeriod"=> 2880
                            );

$resultPostJob = $conn->post('/sms/'. $smsServices[0] . '/jobs/', $content);

$smsJobs = $conn->get('/sms/'. $smsServices[0] . '/jobs/');

echo '{"contacts": $contactsSms,"username": $username, "messageText": $messageText, "result": $resultPostJob, "sms": $smsJobs}'; ?>

EDIT : It seems to work, I don't have the error anymore ! But the SMS is not sent...

A:

From a first reading, your PHP script most probably prints invalid JSON. The following statement will print JSON that contains unquoted strings (assuming one or more of the parameters like $_POST['messsageText'] contain string values):
echo '{"contacts": $contactsSms,"username": $username, "messageText": $messageText, "result": $resultPostJob, "sms": $smsJobs}';

Example output (pretty-printed for readability):
{
  "contacts": foobar,  # <-- This is invalid! It's a string and needs to be quoted!
  "username": johndoe,
  "messageText": Hello World,  # <-- This is even more invalid!
  ...
}

I'd suggest to rely on PHP's json_encode function to generate valid JSON output:
echo json_encode([
    "contacts" => $contactsSms,
    "username" => $username,
    "messageText" => $messageText,
    // etc.
]);

