[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.6

Can anyone find the error in this POST NSMutableURLRequest to a PHP script?

Asked by Cuahutleco in Objective-C Programming Language, PHP Scripting Language, Cocoa Programming Language

Tags: iphone, cocoa, objective-c, php, web services

I try to send data to a php script  that stores the information.

And I'm trying to send a syncronousRequest with Objc code from iPhone using a POST NSMutableURLRequest.

But the answer always returns null values:

> [DEBUG]... response from request:
> There was an error, all field not
> existe, please try again!<br />format
> = <br />category = <br />title = <br />description = <br />photo =<br
> />latitude = <br />longitude =

What am I doing wrong? because what I see is that no field is received.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
PHP SCRIPT:
 
 
mb_internal_encoding("UTF-8");
        
        if(isset($_POST['format']) && isset($_POST['category']) && isset($_POST['title']) && isset($_POST['description']) && isset($_FILES['photo']) {
        	save($_POST['category'], $_POST['title'], $_POST['description'], $_FILES['photo']); 
        } else {
        	echo "There was an error, a field does not exist, please try again!<br />";
        	echo "format = " . $_POST['format'] . "<br />";
        	echo "category = " . $_POST['category'] . "<br />";
        	echo "title = " . $_POST['title'] . "<br />";
        	echo "description = " . $_POST['description'] . "<br />";
        	echo "photo =" . $_FILES['photo'] . ;
        }
    ...
    ...
 
 
OBJ-C Client code:
 
 
 //creating the url request:
    	NSURL *cgiUrl = [NSURL URLWithString:@"http://www.hhh.com/uploading.php"];
    	NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
    	
    	//adding header information:
    	[postRequest setHTTPMethod:@"POST"];
    	
    	NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
    	[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
    	
    	
    	//setting up the body:
    	NSMutableData *postBody = [NSMutableData data];
    	[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"category\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"iPhone"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"format\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
 
// [CORRECTED] It was like this
//   	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] //dataUsingEncoding:NSUTF8StringEncoding]];
 
//NOW
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:self.theCaption] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[NSData dataWithData:UIImageJPEGRepresentation(resizedImage(self.myPhoto, CGRectMake(0, 0, 600, 800)), 0.5)]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
    	[postRequest setHTTPBody:postBody];
    	
    	NSError *theError;
    	NSData *returnData = [ NSURLConnection sendSynchronousRequest: postRequest returningResponse: nil error:&theError ];
    	NSString *returnDataString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    	
    	NSLog(@"[DEBUG]... response from request: %@", returnDataString);
[+][-]10/28/09 04:25 PM, ID: 25689294Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Objective-C Programming Language, PHP Scripting Language, Cocoa Programming Language
Tags: iphone, cocoa, objective-c, php, web services
Sign Up Now!
Solution Provided By: Cuahutleco
Participating Experts: 0
Solution Grade: A
 
[+][-]10/28/09 04:48 PM, ID: 25689404Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625