{"id":7951,"date":"2015-11-12T03:16:31","date_gmt":"2015-11-12T03:16:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/alamofire-post-response-json-nil-using-php-open-source-projects-alamofire-alamofire\/"},"modified":"2015-11-12T03:16:31","modified_gmt":"2015-11-12T03:16:31","slug":"alamofire-post-response-json-nil-using-php-open-source-projects-alamofire-alamofire","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/alamofire-post-response-json-nil-using-php-open-source-projects-alamofire-alamofire\/","title":{"rendered":"Alamofire .POST response JSON nil using PHP-open source projects Alamofire\/Alamofire"},"content":{"rendered":"<p>How can I get JSON values back from my PHP script?<\/p>\n<p>I get <code>nil<\/code> in my JSON response.<\/p>\n<p>The <code>Alamofire.request<\/code> works. (i.e. I receive JSON data at my PHP script.)<\/p>\n<p>The PHP script works. (i.e. I can process the input JSON and I generate a valid JSON string using <code>json_encode<\/code>.)<\/p>\n<p>Here&#8217;s the code&#8230;<\/p>\n<p>Swift:<\/p>\n<pre><code>Alamofire\n    .request(.POST, \"http:\/\/www.myserver.com\/from_swift.php\", parameters: dicMyDictionary, encoding: ParameterEncoding.JSON)\n    .responseJSON {\n        (request, response, JSON, error) in\n        println(\"request: \\(request)\")\n        println(\"response: \\(response)\")\n        println(\"JSON: \\(JSON)\")\n        println(\"error: \\(error)\")\n}\n<\/code><\/pre>\n<p>Println Results:<\/p>\n<pre><code>request:  { URL: http:\/\/www.myserver\/from_swift.php }\n\nresponse: Optional( { URL: http:\/\/www.myserver\/from_swift.php } { status code: 200, headers {\n    Connection = \"Keep-Alive\";\n    \"Content-Type\" = \"application\/json\";\n    Date = \"Fri, 16 Jan 2015 12:02:24 GMT\";\n    \"Keep-Alive\" = \"timeout=5, max=200\";\n    Server = \"Apache\/2.2.29 (Unix)\";\n    \"Transfer-Encoding\" = Identity;\n    \"X-Powered-By\" = \"PHP\/5.4.36\";\n} })\n\nJSON: nil\n\nerror: Optional(Error Domain=NSCocoaErrorDomain Code=3840 \"The operation couldn\u2019t be completed. (Cocoa error 3840.)\" (Invalid value around character 0.) UserInfo=0x7ff3b380db80 {NSDebugDescription=Invalid value around character 0.})\n<\/code><\/pre>\n<p>PHP:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>Solution: I had an error in my PHP script. As a result, the error message was being passed back in the response before the JSON string. As a result, the <code>.responseJSON<\/code> correctly didn&#8217;t recognize the data as JSON.<\/p>\n<p>To help others, when you&#8217;re getting <code>nil<\/code> in your data parameter from <code>.responseJSON<\/code>, troubleshoot by changing <code>.responseJSON<\/code> to <code>.responseString<\/code> to see if you get any results back in the data parameter. For example, when I changed to <code>.responseString<\/code> I saw the following:<\/p>\n<pre><code>JSON: Optional(\"<br \/>\\n<b>Strict Standards<\/b>:  mysqli::next_result(): There is no next result set. Please, call mysqli_more_results()\/mysqli::more_results() to check whether to call this function\/method in <b>\/home\/xxx\/public_html\/from_swift.php<\/b> on line <b>115<\/b><br \/>\\n{\\\"books\\\":[\\\"the hunger games\\\",\\\"mockingjay\\\"]}\")\n<\/code><\/pre>\n<p>Notice, the error message in front of the json string. After I corrected the PHP script and changed back to <code>.responseJSON<\/code>, it worked&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How can I get JSON values back from my PHP script? I get nil in my JSON response. The Alamofire.request works. (i.e. I receive JSON data at my PHP script.) The PHP script works. (i.e. I can process the input JSON and I generate a valid JSON string using json_encode.) Here&#8217;s the code&#8230; Swift: Alamofire [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7951","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7951"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7951\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}