views:

354

answers:

2

I'm working on an iPhone project that needs to receive data from a PHP script during execution. My first thought was to use sockets/streams on either end to connect the two, but I am having trouble finding information on how to do this from the iPhone side.

Has anyone been down this path that could point me towards some useful resources or offer some advice? The official documentation seems to be geared more towards desktop apps and uses code that doesn't seem to be supported on the iPhone (namely NSHost).

Update: The intended use of this app is to receive log messages from an executing script, so I can't use a simple HTTP request with JSON or XML. Many cases will involve the page being loaded by another client, where the script would relay/push log messages to the iPhone.

A: 

Why don't you just use HTTP? Create an ad-hoc protocol with XML or JSON, use POST for upstream data transmission. I'm a fan of JSON for this sort of thing, personally. The PHP, instead of returning a webpage in HTML for rendering, should just return your data in a JSON format.

mythogen
In this case, this wouldn't work for my purposes (added explanation above).
Wilco
So you're looking for something that can open the connection from the script side? You could just use a polling interface on the iPhone side. I think you'd need to implement a Push Notification system to be able to open the connection from the server side.
mythogen
Ideally I'm thinking the iPhone would poll at regular intervals for incoming connection requests and once found, would use a continuous connection lasting the life of the script's execution.As a first step though, I'd be happy leaving out the polling part and have it just listen for 30 or so seconds and accept any incoming data.
Wilco
+2  A: 

Polling is evil. You'll chew through batteries doing that.

You might consider running an HTTP server on the iPhone. Check out this blog post; it has an implementation of an HTTP server in Cocoa as well as example code for using it for two-way communication.

The PHP CURL library (can't link it because the site doesn't trust me yet, just search php.net for it) is a (relatively) simple, easy way to make http requests with a PHP script.

Ryan Ballantyne
Interesting idea - this could prove useful for quite a few different applications.
Wilco