Unrecognized image file type - php

I'm creating an iOS application using AFMultipartFormData AFNetworking to upload an image onto Wordpress site. On the Wordpress server side, the following data was received when I echoed $_FILES:
media = {
error = (
0
);
name = (
"IMG_0004.JPG"
);
"tmp_name" = (
"C:\\Windows\\Temp\\phpF010.tmp"
);
type = (
"image/jpeg"
);
};
Somehow, Wordpress doesn't recognize my file as a valid image file in wp_check_filetype_and_ext() as I'm getting the following error back:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)"
Here is my Wordpress function to handle the file uploaded and insert it into the media directory:
function ldp_image_upload( $request ) {
if ( empty($_FILES) ) {
return new WP_Error( 'Bad Request', 'Missing media file', array( 'status' => 400 ) );
}
$overrides = array('test_form' => false);
$uploaded_file = $_FILES['media'];
$wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
return new WP_Error( 'Unsupported Media Type', 'Invalid image file', array( 'status' => 415 ) );
$file = wp_handle_upload($uploaded_file, $overrides);
if ( isset($file['error']) )
return new WP_Error( 'Internal Server Error', 'Image upload error', array( 'status' => 500 ) );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$filename = basename($file);
// Construct the object array
$object = array(
'post_title' => $filename,
'post_content' => $url,
'post_mime_type' => $type,
'guid' => $url,
);
// Save the data
$id = wp_insert_attachment($object, $file);
if ( !is_wp_error($id) ) {
// Add the meta-data such as thumbnail
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
}
// Create the response object
$response = new WP_REST_Response( array('result' => 'OK'));
return $response;
}
Here is the code on the front-end to send the image:
- (void)createMedia:(RemoteMedia *)media
forBlogID:(NSNumber *)blogID
progress:(NSProgress **)progress
success:(void (^)(RemoteMedia *remoteMedia))success
failure:(void (^)(NSError *error))failure
{
NSProgress *localProgress = [NSProgress progressWithTotalUnitCount:2];
NSString *path = media.localURL;
NSString *type = media.mimeType;
NSString *filename = media.file;
NSString *apiPath = [NSString stringWithFormat:#"sites/%#/media/new", blogID];
NSString *requestUrl = [self pathForEndpoint:apiPath
withVersion:ServiceRemoteRESTApibbPressExtVersion_1_0];
NSMutableURLRequest *request = [self.api.requestSerializer multipartFormRequestWithMethod:#"POST"
URLString:[[NSURL URLWithString:requestUrl relativeToURL:self.api.baseURL] absoluteString]
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
[formData appendPartWithFileURL:url name:#"media[]" fileName:filename mimeType:type error:nil];
} error:nil];
AFHTTPRequestOperation *operation = [self.api HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation * operation, id responseObject) {
NSDictionary *response = (NSDictionary *)responseObject;
NSArray * errorList = response[#"error"];
NSArray * mediaList = response[#"media"];
if (mediaList.count > 0){
RemoteMedia * remoteMedia = [self remoteMediaFromJSONDictionary:mediaList[0]];
if (success) {
success(remoteMedia);
}
localProgress.completedUnitCount=localProgress.totalUnitCount;
} else {
DDLogDebug(#"Error uploading file: %#", errorList);
localProgress.totalUnitCount=0;
localProgress.completedUnitCount=0;
NSError * error = nil;
if (errorList.count > 0){
NSDictionary * errorDictionary = #{NSLocalizedDescriptionKey: errorList[0]};
error = [NSError errorWithDomain:WordPressRestApiErrorDomain code:WPRestErrorCodeMediaNew userInfo:errorDictionary];
}
if (failure) {
failure(error);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogDebug(#"Error uploading file: %#", [error localizedDescription]);
localProgress.totalUnitCount=0;
localProgress.completedUnitCount=0;
if (failure) {
failure(error);
}
}];
// Setup progress object
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
localProgress.completedUnitCount +=bytesWritten;
}];
unsigned long long size = [[request valueForHTTPHeaderField:#"Content-Length"] longLongValue];
// Adding some extra time because after the upload is done the backend takes some time to process the data sent
localProgress.totalUnitCount = size+1;
localProgress.cancellable = YES;
localProgress.pausable = NO;
localProgress.cancellationHandler = ^(){
[operation cancel];
};
if (progress) {
*progress = localProgress;
}
[self.api.operationQueue addOperation:operation];
}
As far as the mimes type is concern, image/jpeg should be supported by wordpress. Unless C:\\Windows\\Temp\\phpF010.tmp is not a true image, then AFNetworking is sending a corrupted file?
Can anyone offer advice on this? Thanks in advance.

Does $wp_filetype['proper_filename'] return something? I haven't tried this but I think this should return the filename as it should be (i.e. with an extension). If it does, you should move the uploaded file to another location and then rename it with the new filename, the upload should then succeed.

Related

invalid registration for fcm in iOS

after google launch their new FCM SDK with iOS capability i was abel to receive the notification from the console well either the app was in background or in active state now i am trying to make my server send the push notification as mentioned here i follow up the documentation steps as mention in https://firebase.google.com/docs/cloud-messaging/server but the result is :
{"multicast_id":XXXXXXXXXXXXXXX,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
The PHP Script
<?php
define( 'API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$registrationIds = array('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json',
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
this code was found on github https://gist.github.com/prime31/5675017 but i make some modification to fit my case
and after searching for related question on stack i just found the following Notification in iOS using new Firebase Messaging SDK, and Invalid Registration Id with my gcm php server but they could not help me in solving my problem.
here is the cliend side but as i mentioned before the app receive the notification from the console.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
float ver = [[[UIDevice currentDevice] systemVersion] floatValue];
if(ver >= 8 && ver<9)
{
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
}else if (ver >=9){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else{
//iOS6 and iOS7 specific code
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
[FIRApp configure];
[self connectToFcm];
// Override point for customization after application launch.
return YES;
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(#"InstanceID token: %#", refreshedToken);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *newUserInfo = [userInfo objectForKey:#"notification"];
NSLog(#"the user info is : %#",newUserInfo);
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = newUserInfo;
localNotification.alertTitle = [newUserInfo objectForKey:#"title"];
localNotification.alertBody = [newUserInfo objectForKey:#"body"];
localNotification.applicationIconBadgeNumber = 0;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else if (state == UIApplicationStateActive) {
AudioServicesPlaySystemSound(1054);
UIImage *icon = [UIImage imageNamed:#"placeholderimage"];
NSString *title = [newUserInfo objectForKey:#"title"];
NSString *body = [newUserInfo objectForKey:#"body"];
notification = [MPGNotification notificationWithTitle:title subtitle:body backgroundColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0f] iconImage:icon];
notification.duration = 5.0;
notification.swipeToDismissEnabled = NO;
[notification show];
}
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#"<>"]];
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"content---%#", token);
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM.");
}
}];
}

Send UTF8 String from UITextField over HTTP link

I'm using php method as web service to add user_comment to mysql database.
<?php
require '../database/connection.php';
extract($_POST);
if (isset($_GET['book_ID'])) {
$book_ID = $_GET['book_ID'];
$user_ID = $_GET['user_ID'];
$theComment = $_GET['theComment'];
}
if (!$db) {
$json[] = array("Message" => "Connection failed");
echo json_encode($json);
}
$sql = mysql_query("INSERT INTO Comment (user_ID , book_ID , theComment) VALUES ('$user_ID','$book_ID','$theComment')");
mysql_query($sql, $db);
$json[] = array("Message" => "Done");
echo json_encode($json);
everything fine if I type English but I have problem with Arabic
Objective-C:
-(NSString *)addCommentForBook:(NSString *)bookID userID:(NSString *)userID theComment:(NSString *)theComment{
NSString *dt = [NSString stringWithFormat:#"?book_ID=%#&user_ID=%#&theComment=%#",bookID,userID,theComment];
NSURL *myURL = [[NSURL alloc]initWithString:[NSString stringWithFormat:#"http://www.myweb.com/library/Books/addCommentForBook.php%#",dt]];
NSMutableDictionary *theArray;
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
if (myData) {
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
theArray = (NSMutableDictionary *)myJSON;
}
return [[theArray valueForKey:#"Message"]objectAtIndex:0];
}
If I test from Browser work php work fine with Arabic but from iOS not work
Any one tell me what's wrong or do I need to convert the string from UITextField first or?

Upload multiple images using AFNetworking in swift

I want to upload multiple images to my website using AFNetworking in swift, but only the last image in the array uploaded .
swift script:
let url = "http://pathtomysite"
let afHTTP : AFHTTPRequestSerializer = AFHTTPRequestSerializer()
let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in
var i = 0
for image in upImage {
let imageData : NSData = UIImageJPEGRepresentation(image as UIImage, 0.5)!
formData.appendPartWithFileData(imageData, name: "uploaded_file", fileName: "imagex\(i)x.png", mimeType: "image/png")
i++
}
}, error: nil)
let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, AnyObject, error) -> Void in
if (error != nil){
print("error")
}
}
uploadTask.resume()
php script:
<?php
$dt = date("Ymdhis");
$fileInfo = pathinfo($_FILES['uploaded_file']['name']);
$file_path = "uploads/";
$file_path = $file_path . basename($_FILES['uploaded_file']['name']);
if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
rename($file_path, 'uploads/' . $dt . '.' . $fileInfo['extension']);
}
?>
try this:
let url = "YOUR_URL"
let afHTTP : AFHTTPRequestSerializer = AFHTTPRequestSerializer()
let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in
for (index, image) in upImage.enumerate() {
let imageData = UIImageJPEGRepresentation(image as UIImage, 0.5)!
formData.appendPartWithFileData(imageData, name: "uploaded_file[]", fileName: "imagex\(index)x.png", mimeType: "image/png")
}
}, error: nil)
let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, AnyObject, error) -> Void in
if (error != nil){
print("error")
}
}
uploadTask.resume()
and change your script to the following:
foreach ($_FILES["uploaded_file"]["tmp_name"] as $index => $tmp_name) {
$filePath = "uploads/" . basename($_FILES["uploaded_file"]["name"][$index]);
if (move_uploaded_file($tmp_name, $filePath)) {
// rename like you want to
}
}
the important piece is adding brackets to the uploaded_file[] in your upload code. if you do not include the [] every image upload overrides the last one.
the other important piece is the foreach loop in your script that handles multiple uploaded images instead of just one.

Upload an image to PHP with Swift 2.0

I've searched for a long time now and nothing works out for me.
I've even tried Alamofire.
I'm trying to upload an image in an iOS App (Swift 2.0) to a Wordpress Photo-Contest plugin through PHP. The PHP script is also used for the Wordpress website to upload images.
This is the PHP:
<?php
define('WP_USE_THEMES', false);
require_once("../../../../../wp-load.php");
$username = $_POST["username"];
$m = contest_upload_photo('contest-upload-photo', 'contest_upload_photo',username_exists($username ));
function contest_upload_photo($atts, $content = null,$user_ID=null) {
//Important variables
if ($user_ID == null){
die();
}
$html = '';//Inciate output string
$koncovky = array('jpg', 'jpeg', 'png', 'gif');
$number_images = get_user_meta($user_ID, 'contest_user_images', true);
if(empty($number_images)){$number_images=0;}
$error = array();
// Do some minor form validation to make sure there is content
$name = trim($_POST['photo-name']);
/*if (empty($_POST['photo-title'])){
$error['title'] = __('Please enter the photo title','photo-contest');
} else {
$title = trim($_POST['photo-title']);
}*/
//Check photo
if ($_FILES['contest-photo']['error'] == UPLOAD_ERR_NO_FILE){
$error['photo'] = __('Please select the image','photo-contest');
} else {
//Control upload and extension
if ($_FILES['contest-photo']['error']) {
$error['upload_error'] = __('Error image upload.','photo-contest');
}
elseif (!in_array(strtolower(pathinfo($_FILES['contest-photo']['name'], PATHINFO_EXTENSION)), $koncovky)) {
$error['extension_error'] = __('Image must be jpg, png or gif.','photo-contest');
}
elseif (!($imagesize = getimagesize($_FILES['contest-photo']['tmp_name'])) || $imagesize[2] > 3) {
$error['type_error'] = __('Image type must be jpg, png or gif.','photo-contest');
}
else {
#$img=getimagesize($_FILES['contest-photo']['tmp_name']);
$minimum = array('width' => '400', 'height' => '400');
$width= $img[0];
$height =$img[1];
if ($width < $minimum['width'] ){
$error['type_error'] = __('Minimum image width is 400px.','photo-contest');
}
elseif ($height < $minimum['height']){
$error['type_error'] = __('Minimum image height is 400px.','photo-contest');
}
$photo_limit = get_option( 'pcplugin-photo-limit', true );
$size_maxi = $photo_limit;
$size = filesize($_FILES['contest-photo']['tmp_name']);
if($size>$size_maxi){
$error['size_error'] = __('File size is above allowed limitations!','photo-contest');
}
}
}
if(empty($error)){
//If no exist error - create attachment post
if(empty($_POST['photo-description'])){
$description = sanitize_text_field($_POST['photo-description']);
}else{
$description = '';
}
#$wp_filetype = wp_check_filetype(basename($_FILES['contest-photo']['name']), null );
#$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $_FILES['contest-photo']['name'] ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => $name,
'post_content' => $description,
'post_status' => 'inherit'
);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( 'contest-photo', 0,$attachment );
$attach_data = wp_generate_attachment_metadata( $attach_id, $wp_upload_dir['url'] . '/' . basename( $_FILES['contest-photo']['name']) );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($attach_id,'contest-active',1);
update_post_meta($attach_id,'contest-photo-points',0);
update_post_meta($attach_id,'contest-photo-author',$user_ID);
update_post_meta($attach_id,'post_author',$user_ID);
$number_images = $number_images+1;
update_user_meta($user_ID, 'contest_user_images', $number_images);
$my_post = array(
'ID' => $attach_id,
'post_author' => $user_ID,
);
wp_update_post( $my_post );
$image = get_post( $attach_id );
if ($attach_id==""){
die("306");
}else{
echo($attach_id);
}
if ( ! $image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) )
die( json_encode( array( 'error' => sprintf( __( 'Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails' ), esc_html( $attach_id ) ) ) ) );
$fullsizepath = get_attached_file( $image->ID );
if ( false === $fullsizepath || ! file_exists( $fullsizepath ) )
// #set_time_limit( 900 ); // 5 minutes per image should be PLENTY
$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
if ( is_wp_error( $metadata ) )
if ( empty( $metadata ) )
wp_update_attachment_metadata( $image->ID, $metadata );
return $attach_id;
}
}
?>
And this is my function in SWIFT:
func send()
{
let imageData :NSData = UIImageJPEGRepresentation(globalImage, 1.0)!;
var request: NSMutableURLRequest?
let HTTPMethod: String = "POST"
let timeoutInterval: NSTimeInterval = 60
let HTTPShouldHandleCookies: Bool = false
let postString = "username=\(globalUsr)"
request = NSMutableURLRequest(URL: NSURL(string: "***URL TO upload.php***")!)
request!.HTTPMethod = HTTPMethod
request!.timeoutInterval = timeoutInterval
request!.HTTPShouldHandleCookies = HTTPShouldHandleCookies
request!.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
let contentType = "multipart/form-data; boundary=\(boundary)"
request!.setValue(contentType, forHTTPHeaderField:"Content-Type")
let body = NSMutableData();
let tempData = NSMutableData()
let fileName = "\(globalImage.description).jpg"
let parameterName = "contest-photo"
let mimeType = "application/octet-stream"
tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
let fileNameContentDisposition = "photo-description=\(fileName)"
let contentDisposition = "Content-Disposition: form-data; contest-photo=\"\(parameterName)\"; \(fileNameContentDisposition)\r\n"
tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("Content-Type: \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData(imageData)
tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(tempData)
body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request!.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request!.HTTPBody = body
do {
let data = try NSURLConnection.sendSynchronousRequest(request!, returningResponse: nil)
print("\(data)-Data")
} catch (let vl_error) {
print("\(vl_error)-Error")
}
}
After a lot back and forth, I've messed up the code a lot and also copied some code from other people that I've found just to make it work.
I do get "<>-Data" printed out in the console.
I would appreciate any help or hint.
After talking in comments, you needed a working example to post your username value to the server.
This is a java REST web service:
#POST
#Produces(MediaType.TEXT_PLAIN)
#Path("/swiftCalculator")
public String swiftCalculator(#FormParam("x") int x, #FormParam("y") int y){
System.out.println("x="+x);
System.out.println("y="+y);
return (x + y) + "";
}
And this is how you call it from your swift client:
func testPost(sender: UIButton) {
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8080/iOSServer/ios/helloworld/swiftCalculator")!)
request.HTTPMethod = "POST"
let d = "4"
let data = "x=4&y=\(d)"
request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
print("data =\(data)")
}
if let response = response {
print("response = \(response)")
}
})
task.resume()
}
Please notice that the script expects to have the values in post, so what you were doing is passing the username to the url and then ask your php script to find it in the post variables. that is why you were getting null in you php script.
In this example, i tried to post two variables, which are x and y from my swift client to my REST jersey web service.
Hope this helps
I've adjusted the function in swift which works fine so far now.
I only need to know how to upload a file to the php script, I've tried different approaches but it now gives me the error that the Image must be jpg, png or gif.
Username, photo name and description get accepted by the script.
func send() {
let request = NSMutableURLRequest(URL: NSURL(string: "***URL***")!)
request.HTTPMethod = "POST"
let postString = "username=\(globalUsr)&photo-name=\(globalImage.description)&photo-description=\(message.text)"
let myData : NSData! = postString.dataUsingEncoding(NSUTF8StringEncoding)
let imageData :NSData = UIImagePNGRepresentation(globalImage, 1.0)!;
let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
let contentType = "multipart/form-data; boundary=\(boundary)"
let body = NSMutableData();
let tempData = NSMutableData()
let fileName = "\(globalImage.description).jpg"
let parameterName = "contest-photo"
let mimeType = "application/octet-stream"
tempData.appendData(myData)
tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
let fileNameContentDisposition = "name=\(fileName)"
let contentDisposition = "Content-Disposition: form-data; name=\"\(fileName)\"; \(imageData)\r\n"
tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("Content-Type: \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData(imageData)
tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(tempData)
body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request.HTTPBody = body
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString!)")
}
task.resume()

Validating appReceiptStoreURL Returning 21002 Status

I have created a class which handles the purchases on In-App Purchases and also the validating of receipts. A while ago I used to use the transactionReceipt property on an SKPaymentTransaction, but have updated my code a fair amount and now use appStoreReceiptURL on the [NSBundle mainBundle].
Basically it seems as though my receipt is being sent to Apple's server in an acceptable manner, but I keep getting the status code of 21002. In auto-renewable subscriptions I know that this means the receipt is not in an acceptable format, however I have no idea what this status means in regard to an in-app purchase receipt.
Here is the local method validating the receipt:
/**
* Validates the receipt.
*
* #param transaction The transaction triggering the validation of the receipt.
*/
- (void)validateReceiptForTransaction:(SKPaymentTransaction *)transaction
{
// get the product for the transaction
IAPProduct *product = self.internalProducts[transaction.payment.productIdentifier];
// get the receipt as a base64 encoded string
NSData *receiptData = [[NSData alloc] initWithContentsOfURL:[NSBundle mainBundle].appStoreReceiptURL];
NSString *receipt = [receiptData base64EncodedStringWithOptions:kNilOptions];
NSLog(#"Receipt: %#", receipt);
// determine the url for the receipt verification server
NSURL *verificationURL = [[NSURL alloc] initWithString:IAPHelperServerBaseURL];
verificationURL = [verificationURL URLByAppendingPathComponent:IAPHelperServerReceiptVerificationComponent];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:verificationURL];
urlRequest.HTTPMethod = #"POST";
NSDictionary *httpBody = #{#"receipt" : receipt,
#"sandbox" : #(1)};
urlRequest.HTTPBody = [NSKeyedArchiver archivedDataWithRootObject:httpBody];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
// create a block to be called whenever a filue is hit
void (^failureBlock)(NSString *failureMessage) = ^void(NSString *failureMessage)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:
^{
// log the failure message
NSLog(#"%#", failureMessage);
// if we have aready tried refreshing the receipt then we close the transaction to avoid loops
if (self.transactionToValidate)
product.purchaseInProgress = NO,
[[SKPaymentQueue defaultQueue] finishTransaction:transaction],
[self notifyStatus:#"Validation failed." forProduct:product],
self.transactionToValidate = nil;
// if we haven't tried yet, we'll refresh the receipt and then attempt a second validation
else
self.transactionToValidate = transaction,
[self refreshReceipt];
}];
};
// check for an error whilst contacting the server
if (connectionError)
{
failureBlock([[NSString alloc] initWithFormat:#"Failure connecting to server: %#", connectionError]);
return;
}
// cast the response appropriately
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
// parse the JSON
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
// if the data did not parse correctly we fail out
if (!json)
{
NSString *responseString = [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
NSString *failureMessage = [[NSString alloc] initWithFormat:#"Failure parsing JSON: %#\nServer Response: %# (%#)",
data, responseString, #(httpResponse.statusCode)];
failureBlock(failureMessage);
return;
}
// if the JSON was successfully parsed pull out status code to check for verification success
NSInteger statusCode = [json[#"status"] integerValue];
NSString *errorDescription = json[#"error"];
// if the verification did not succeed we fail out
if (statusCode != 0)
{
NSString *failureMessage = [[NSString alloc] initWithFormat:#"Failure verifying receipt: %#", errorDescription];
failureBlock(failureMessage);
}
// otherwise we have succeded, yay
else
NSLog(#"Successfully verified receipt."),
[self provideContentForCompletedTransaction:transaction productIdentifier:transaction.payment.productIdentifier];
}];
}
The important PHP function on the server does this:
/**
* Validates a given receipt and returns the result.
*
* #param receipt Base64-encoded receipt.
* #param sandbox Boolean indicating whether to use sandbox servers or production servers.
*
* #return Whether the reciept is valid or not.
*/
function validateReceipt($receipt, $sandbox)
{
// determine url for store based on if this is production or development
if ($sandbox)
$store = 'https://sandbox.itunes.apple.com/verifyReceipt';
else
$store = 'https://buy.itunes.apple.com/verifyReceipt';
// set up json-encoded dictionary with receipt data for apple receipt validator
$postData = json_encode(array('receipt-data' => $receipt));
// use curl library to perform web request
$curlHandle = curl_init($store);
// we want results returned as string, the request to be a post, and the json data to be in the post fields
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postData);
$encodedResponse = curl_exec($curlHandle);
curl_close($curlHandle);
// if we received no response we return the error
if (!$encodedResponse)
return result(ERROR_VERIFICATION_NO_RESPONSE, 'Payment could not be verified - no response data. This was sandbox? ' . ($sandbox ? 'YES' : 'NO'));
// decode json response and get the data
$response = json_decode($encodedResponse);
$status = $response->{'status'};
$decodedReceipt = $response->{'receipt'};
// if status code is not 0 there was an error validation receipt
if ($status)
return result(ERROR_VERIFICATION_FAILED, 'Payment could not be verified (status = ' . $status . ').');
// log the returned receipt from validator
logToFile(print_r($decodedReceipt, true));
// pull out product id, transaction id and original transaction id from infro trurned by apple
$productID = $decodedReceipt->{'product_id'};
$transactionID = $decodedReceipt->{'transaction_id'};
$originalTransactionID = $decodedReceipt->{'original_transaction_id'};
// make sure product id has expected prefix or we bail
if (!beginsWith($productID, PRODUCT_ID_PREFIX))
return result(ERROR_INVALID_PRODUCT_ID, 'Invalid Product Identifier');
// get any existing record of this transaction id from our database
$db = Database::get();
$statement = $db->prepare('SELECT * FROM transactions WHERE transaction_id = ?');
$statement->bindParam(1, $transactionID, PDO::PARAM_STR, 32);
$statement->execute();
// if we have handled this transaction before return a failure
if ($statement->rowCount())
{
logToFile("Already processed $transactionID.");
return result(ERROR_TRANSACTION_ALREADY_PROCESSED, 'Already processed this transaction.');
}
// otherwise we insert this new transaction into the database
else
{
logToFile("Adding $transactionID.");
$statement = $db->prepare('INSERT INTO transactions(transaction_id, product_id, original_transaction_id) VALUES (?, ?, ?)');
$statement->bindParam(1, $transactionID, PDO::PARAM_STR, 32);
$statement->bindParam(2, $productID, PDO::PARAM_STR, 32);
$statement->bindParam(3, $originalTransactionID, PDO::PARAM_STR, 32);
$statement->execute();
}
return result(SUCCESS);
}
The actual PHP script being executed is:
$receipt = $_POST['receipt'];
$sandbox = $_POST['sandbox'];
$returnValue = validateReceipt($receipt, $sandbox);
header('content-type: application/json; charset=utf-8');
echo json_encode($returnValue);
Comparing your PHP with mine (which I know works) is difficult because I am using HTTPRequest rather than the raw curl APIs. However, it seems to me that you are setting the "{receipt-data:..}" JSON string as merely a field in the POST data rather than as the raw POST data itself, which is what my code is doing.
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postData); // Possible problem
$encodedResponse = curl_exec($curlHandle);
Compared to:
$postData = '{"receipt-data" : "'.$receipt.'"}'; // yay one-off JSON serialization!
$request = new HTTPRequest('https://sandbox.itunes.apple.com/verifyReceipt', HTTP_METH_POST);
$request->setBody($postData); // Relevant difference...
$request->send();
$encodedResponse = $request->getResponseBody();
I have changed my variable names a bit to make them match up with your example.
the code 21002 means "The data in the receipt-data property was malformed or missing."
you can find it in
https://developer.apple.com/library/archive/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
below code is my class for appstore in-app verifyRecepip, GuzzleHttp is required, you can install it by composer require guzzlehttp/guzzle https://github.com/guzzle/guzzle
<?php
namespace App\Libraries;
class AppStoreIAP
{
const SANDBOX_URL = 'https://sandbox.itunes.apple.com/verifyReceipt';
const PRODUCTION_URL = 'https://buy.itunes.apple.com/verifyReceipt';
protected $receipt = null;
protected $receiptData = null;
protected $endpoint = 'production';
public function __construct($receipt, $endpoint = self::PRODUCTION_URL)
{
$this->receipt = json_encode(['receipt-data' => $receipt]);
$this->endpoint = $endpoint;
}
public function setEndPoint($endpoint)
{
$this->endpoint = $endpoint;
}
public function getReceipt()
{
return $this->receipt;
}
public function getReceiptData()
{
return $this->receiptData;
}
public function getEndpoint()
{
return $this->endpoint;
}
public function validate($bundle_id, $transaction_id, $product_code)
{
$http = new \GuzzleHttp\Client([
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'timeout' => 4.0,
]);
$res = $http->request('POST', $this->endpoint, ['body' => $this->receipt]);
$receiptData = json_decode((string) $res->getBody(), true);
$this->receiptData = $receiptData;
switch ($receiptData['status']) {
case 0: // verify Ok
// check bundle_id
if (!empty($receiptData['receipt']['bundle_id'])) {
$receipt_bundle_id = $receiptData['receipt']['bundle_id'];
if ($receipt_bundle_id != $bundle_id) {
throw new \Exception('bundle_id not matched!');
}
}
// check transaction_id , product_id
if (!empty($receiptData['receipt']['in_app'])) {
$in_app = array_combine(array_column($receiptData['receipt']['in_app'], 'transaction_id'), $receiptData['receipt']['in_app']);
if (empty($in_app[$transaction_id])) {
throw new \Exception('transaction_id is empty!');
}
$data = $in_app[$transaction_id];
if ($data['product_id'] != $product_code) {
throw new \Exception('product_id not matched!');
}
} else {
$receipt_transaction_id = $receiptData['receipt']['transaction_id'];
$receipt_product_id = $receiptData['receipt']['product_id'];
if ($receipt_transaction_id != $transaction_id || $product_id != $product_code) {
throw new \Exception('tranaction_id not matched!');
}
}
break;
case 21007:// sandbox order validate in production will return 21007
if ($this->getEndpoint() != self::SANDBOX_URL) {
$this->setEndPoint(self::SANDBOX_URL);
$this->validate($bundle_id, $transaction_id, $product_code);
} else {
throw new \Exception('appstore error!');
}
break;
default:
throw new \Exception("[{$receiptData['status']}]appstore error!");
break;
}
return $receiptData;
}
}
I think Morteza M is correct. I did a test and got reply(JSON) like:
{
'status':
'environment': 'Sandbox'
'receipt':
{
'download_id':
....
'in_app":
{
'product_id':
....
}
....
}
}

Categories