對於我的生活,我似乎無法弄清楚爲什麼會發生這種情況。我們從一個iOS應用程序發送正文到一個php後端POST API,寫入一個MYSQL服務器。我們已經將請求主體打印到一個txt文件中,並且顯然獲取了所有數據,但是當我們嘗試存儲它時,它每次都會失敗。如果我們從iOS應用程序發送不包含Base64編碼字符串的數據,它會將數據存儲到數據庫中。我們試圖將字符串的Base64存儲LONGTEXT格式:POST多個參數到PHP到MYSQL數據庫
<?php
include('db_connect.php');
$http_data = file_get_contents('php://input'); //This is json data
$data = json_decode($http_data);
$b64img1 = $data->img1; //A Base64 Encoded String
$b64img2 = $data->img2; //A Base64 Encoded String
$img1 = $data->img1Title;
$img2 = $data->img2Title;
$cat = $data->category;
$user = $data->userID;
$current = date('Y-m-d H:i:s');
$time = $data->length;
$length = date('Y-m-d H:i:s', time() + $time);
$postID = $current . '_' . $user . '_' . $length;
$loc = mysql_query("SELECT * FROM users WHERE BINARY userID = '$user';");
$row = mysql_fetch_array($loc);
$location = $row['location'];
$numPosts = $row['post_number'];
$posts = $row['postIDs'];
$newNum = $numPosts + 1;
$newPosts = $posts . $postID . ', ';
mysql_query("INSERT INTO `content` (`postID`,`img1`,`img2`,`img1title`,`img2title`,`category`,`user`,`location`,`postUntil`) VALUES ('$postID','$b64img1','$b64img2','$img1','$img2','$cat','$user','$location','$length');");
mysql_query("UPDATE users SET post_number = '$newNum', postIDs = '$newPosts' WHERE BINARY userID = '$user';");
echo "{\"Success\":\"Yes\"}";
mysql_close($con);
exit;
?>
這裏是我們用來發送到我們的POST API iOS的代碼...
NSData *dataImage = [[NSData alloc] init];
NSData *dataImage2 = [[NSData alloc] init];
UIImage *selectedImage = info[UIImagePickerControllerEditedImage];
dataImage = UIImagePNGRepresentation(selectedImage);
NSString *image = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
dataImage2 = UIImagePNGRepresentation(selectedImage);
NSString *image2 = [dataImage2 base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *dictionaryOrArrayToOutput = @{@"img1" : image,
@"img2" : image2,
@"img1title" : titleOne,
@"img2title" : titleTwo,
@"category" : category,
@"userID" : userID,
@"length" : time};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArrayToOutput
options:NSJSONWritingPrettyPrinted
error:&error];
if (! jsonData)
{
NSLog(@"Got an error: %@", error);
}
else
{
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);
NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:SAMPLEURL]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
NSLog(@"REPLY FROM SERVER: %@", requestReply);
}
有誰看到的東西不對,或者可以指向正確的方向?這讓我瘋狂,似乎無法找到一個原因,這是行不通的。
把問題解決一點。確保您正確地將JSON發佈到服務器,使用Charles Proxy來驗證。在收到數據後立即在服務器上記錄並在解序列化之後記錄,確保在這些點上有正確的數據。然後,如果所有這些都是正確的,請查看MySQL代碼。如果仍有問題,請將日誌結果添加到問題中。 – zaph 2014-10-31 04:01:25