How i use Php in iphone - iphone

I am new in iphone,in my app required data from the server.
1)mysql database uploaded on the server
2)With the help of php code i fetch the data from the server
problem is that how i integrate php in my iphone please solve my problem,
give any reference.
Thanks,

I'm not sure to understand your problem, but you can't use PHP on iPhone.
If you need data from an external database, create web services to get the data in JSON, XML, or what suits you best.
Your iPhone application will connect to a specific URL, and get the data from a (PHP) script that will query the database and return the result in a specific format.

The best way in my opinion is to use JSON (see http://code.google.com/p/json-framework/).
You can make requests to your server using this code
NSError *error;
NSString *post =[NSString stringWithFormat:#"any-data=%#",
myData];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://your-server.com/your-script.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#",data);
NSDictionary* parsedData = [data JSONValue];
You then can access the data using
[parsedData valueForKeyPath:#"myVar"]
Hope this hepls,
Miguel

You could look at passing arrays of objects from the server to the iPhone in the form of a plist file. Have the server generate the plist in the appropriate format on a url and then on the device:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com/myplist.php"];
NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com/myplist.php"];
If your unsure what the format is for plist files create one in your xcode project and use that as a template.

Related

How to post multiple data to web server

I have below code for posting data to the web server but it gives me null response, I have multiple data and they are:-
date,
rig,
driller,
offsider-1,
offsider-2,
shift
,
project,
supervisoremail,
geologistemail,
comments,
plants[0][name-id],
plants[0][start-usage],
plants[0][end-usage],
consumables[0][category],
consumables[0][name-id],
consumables[0][used],
consumables[0][code],
consumables[0][description],
I have to post this data and I used this code for posting but it didn't work for me. Please help me out.
NSURL *url=[NSURL URLWithString:#"http://www.mydor.com.au/staff/dor/idorInsert"];
NSString *post =[[NSString alloc]initWithFormat:#"date=15/08/2012&rig=53&driller=207&offsider-1=131&offsider- 2=236&shift=day&project=24&supervisoremail=24&geologistemail=24&comments=&plants[0][name- id]=286&plants[0][start-usage]=1.0&plants[0][end-usage]=0.0"];
NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"here u re");
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"here also called");
NSLog(#"%#",data)
Thanks in advance.
Here is an article which demonstrates how to consume .NET Web service using iOS application. You can use the techniques discussed in the article:
http://www.highoncoding.com/Articles/809_Consuming__NET_Web_Services_in_an_iOS_Application.aspx
Almost all the work I do involves .NET with iOS. Have a look here for the network class I wrote and use all the time (AFNetworking based). I prefer to use a regular aspx form instead of an asmx web-service. I do this because I don't want to expose any web-methods to snooping visitors and I use an API key check before returning anything in the aspx form. I can send you an example .NET code (written in VB.NET) if you need me to.

Json Request in iphone

Actually I am working on the Couchdb .Till now i have used the Local server for Connecting to he CouchDb . Now I need to Connect to the remote server of couchdb . For this I need to send the User Name And Password Through the Json .
How can we use authenticated basic Json post ?
use the post method (post the username and password )
code like …. try
NSString *postStr = [NSString stringWithFormat:#"username=%#&password=%#", username, password];
NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *requesting = [[NSMutableURLRequest alloc] initWithURL:YourURLObject];
[requesting setHTTPMethod:#"POST"];
[requesting setValue:[NSString stringWithFormat:#"%d", [postData length]] forHTTPHeaderField:#"Content-Length"];
[requesting setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[requesting setHTTPBody:postData];
NSError *error;
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:requesting returningResponse:&response error:&error];
Add JSON framework if you are using iOS5 sdk else download SBJSON and add it to your project and import "SBJSON.h"
Case 1: Using JSON Array
NSArray *credentials = [NSArray arrayWithObjects:#"uName", #"pWord",nil];
NSString *jsonString = [credentials JSONString];
Case 2: Using JSON Dictionary
NSDictionary *credentials = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"uName", #"pWord",nil] forKeys:[NSArray arrayWithObjects:#"UserName",#"password",nil]];
NSString *jsonString = [credentials JSONString];

How to send the updated data using json to server in iphone?

am using web services (JSON). from json am getting data this data loading into tableview am trying to edit this data but after edit the data how to send this updated data to server.
please any one help me?
try this will help you.. this is the post method for updating data in WS .
NSString *post =[NSString stringWithFormat:#"uid=%#&firstname=%#&lastname=%#&phone=%#&bday=%#&about_me=%#&image=%#&image_code=%#&contact_number=%#",LoginID,fname,lname,cn,bday,abtme,strimage11,c11,cn];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"YOUR LINK"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *uData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:uData encoding:NSUTF8StringEncoding];
//
NSMutableDictionary *temp = [data JSONValue];
//
NSLog(#"%#",temp);
I am assuming you have your edited data, then
Form your json. There are several libraries out there which can help you.
Know the hostname of your server.
Know which API to hit on your server.
then pass this json as POST (GET is also ok, but POST is preferred).
Process this received json on your server.
Hope this helps. Nothing much to it actually.

How to access a SOAP webservice from my iPhone app

I am new to iPhone application development and also this stackoverflow,if I made any mistakes sorry. I have some of the questions
1) Do I need to install any other external framework into my application. I am using SDK Simulator 4.2
2) I have gone through some of the material which was posted here, I found some code and I made some modification to suits my application When I am requesting for a URL it is returning me NULL.
3) My task is to send a three parameters into database.
NSString *post =[NSString stringWithFormat:#"?FirstName=%#?Surname=%#?IDNumber=%#?DOB=%#",namestring,surnamestring,cidstring,dobstring];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSLog(#"Post Length:",[postData length]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.esoft.co.za/MDS/Service1.asmx?op=InsertSuppliers"]];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"response data is: %#",[responseData length] );
NSString *data = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#",data);
Thanks in Advance Guys...Please provide me any sample code or material..
I think it's because you're trying to send invalid request. Just try to check and log error message to see what's wrong.
What you're trying to do is to send POST data over GET request, which is wrong anyway. I'm also not sure about those "Post" data format, but when you'll check error message you'll find what's wrong.
NSLog(#"Request failed with error: %#", [err localizedDescription]);
best

Problems with Facebook-Api publish_stream in iPhone application

I've added Facebook Connect to my new iPhone application. Everything perfect, no problem.
In this application, however, I need to post on user's Wall without prompt any dialog box.
I've searched in Facebook Documentation and, from what I understand, if I ask user to give me the right permission (in this case, publish_stream), the dialog box should no longer appear.
But the box appears, despite of all.
I hope you can help me.
Thank you.
P.S. Sorry for my bad English
Use the graph API after you have acquired the publish_stream permission. You make a POST to:
https://graph.facebook.com/ID/feed
This iPhone SDK does not support this natively, you will have to implement this yourself. You will need to ensure that you create the proper JSON encoded parameters and ensure they are properly escaped.
A good place to start with this is here.
Thank you!
So, if I understand well, I have to do something like this:
Use this framework http://code.google.com/p/json-framework/ to add JSON support.
And this code:
SBJSON *json = [SBJSON new];
json.humanReadable = YES;
NSString *service = #"NameService";
NSMutableDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"MessaggeFromMyApp",#"message",
#"http://www.sample.com",#"link",
#"nomeOfTheLink",#"name",
#"captionOfTheLink",#"caption",
#"descriptionofTheLink",#"description",
#"MyDistrict",#"value",
#"2",#"txs_Action",
nil];
//Pass it twice to escape quotes
NSString *jsonString = [NSString stringWithFormat:#"%#", [params JSONFragment], nil];
NSString *changeJSON = [NSString stringWithFormat:#"%#", [jsonString JSONFragment], nil];
NSLog(jsonString);
NSLog(changeJSON);
NSString *requestString = [NSString stringWithFormat:#"{\"id\":15,\"method\":\"%#\",\"params\":[%#]}",service,changeJSON,nil];
NSLog(requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: #"https://graph.facebook.com/me/feed"]];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setHTTPMethod: #"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody: requestData];
//Data returned by WebService
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(returnString);
Thank you again.