Getting Objective-C Objects in PHP - php

I am sending an NSData Object to the server script running PHP.Is it possible to collect that object in PHP ?
Is it possible to get NSArray,NSString, NSData or any Custom Objects (Subclass of NSObject) in PHP ?
Thanks

Look for a method for serializing and deserializing your data.
Facebook uses a protocol called Thrift Google uses protocol buffers.
If you don't need to have an industry standard solution, you can come up with your own way to tag values with meta data in order to determine how PHP will handle your data. PHP doesn't (at least that I'm aware of) have all the niftness that objective C has with its objects and inheritance, but PHP has a function or library for almost anything you will need to do.
One approach might be to use JSON to store type/value pair for each parameter you want to process.

Related

Sending a PHP object to another PHP page

I'd like to know if there's a way by which a PHP object could be sent to another PHP page on a different machine?
For instance - I've implemented a class that constructs a Trie. Now after the Trie has been constructed , I'd like to send across the object to another PHP page so that it can access the object too.
Would probably packaging it into some sort of encoded JSON request and then sending it to a page which could relay it to the required page using jQuery , be a feasible option ?
I'm sorry I'm absolutely new to this !
Will Appreciate any help provided.
Cheers!
An object is an instance of a Class. Objects can't be sent around as they are. A "transmissible" object must be Serializable: make sure that your class implements Serializable
Once you've implemented the interface, just call the serialize and deserialize methods to get the object-string and to rebuild the object
Use php functions serialize() and unserialize(). When you unserialize the object, be sure to have its class defined.
personally I'd store the object in an object store rather than serialise memcache, APC even a session can be used, you can also use nosql style databases and key stores all of which are pretty much perfectly suited to object persistance as they're mostly very fast access data stores without the sql overhead. nosql-database.org
Just assign each object a key based off the key of the user browsing generate a new key for each new browser and store it in session/cookie to retrieve their personal "objects"
As already mentioned by STT you can serialise and store in the session thats perfectly fine although serialise is retarded to implement in php they should never have put that in.
Instead look at APC and SPL both are built in to php (APC is more suited for an object store especially since from 5.6 (I believe so onwards) its no longer an extension but built into the PHP core its self making it fully native so you get not only a simple object store but also op code cache which will seriously increase the speed of your php pages.
Note: APC is only really usable for object store when you run a single web / php server if you need multiple processing servers then you'll need a distributed object store in which case the best you can probably get is memcache
Lots of links mostly all on SO.
When should I use Memcache instead of Memcached?
Memcached vs APC which one should I choose?
http://php.net/manual/en/book.memcached.php
http://php.net/manual/en/book.apc.php
Is there a way in PHP to use persistent data as in Java EE? (sharing objects between PHP threads) without session nor cache/DB
The right way is to implement Serializable interface in the class of your object (if it doesn't yet) that pass it through some sort of transport between two servers.
Try to not send data trough client-side code unless you trust client or you don't care about data.

Library to write PHP web service for android consumption [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Framework for providing API access to website?
I need to write a service in PHP. The server will be used by android/iphone clients through the url for example like this
http://www.myservice.com/query.php?param1=a&param2=2...
The server will return data back
The client will push data to server
There can be large num of clients simultaneously accessing so the performance is key
I want to use the data format that is easily understood by my android client. In other words, I do not want to reinvent the wheel and create my own format and parsing, instead I would prefer to use any library if it exists.
Is there a framework that I can use to abstract the communication mechanism for data get and push ?
Thanks,
Ahmed
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from Java like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Get it here.
Being the Author of Restler, I would like to suggest that you can try Restler 3 for the following reasons
It is specifically made for API creation
It handles media type conversion for you and supports many media types including
JSON
Plist (both XML and Binary)
XML
Comes with many examples to get started.
Your API is automatically documented with Restler API Explorer
well,
Use mvc framework(yii, ci, ...)
and from controller directly print json_encode($object).
It will return json data to browser, and consume it anywhere(compuer, droid, iphone, ... , iron :D). the solution is json. just share object in json format, so anyone can map it into it's preferred obejct
here is something something you may have a look

How to deal with the 'DataSet' data structure in php?

I'm trying to do a call to a soap webservice from php.
The webservice returns an ADO.NET DataSet structure. Are there any libraries in PHP that can deal with this sort of data structures? If so, what are they called and where can I find them? If not, feel free to give tips?
So far, I have this (using ActiveMQ and the NuSoap library):
/**
* Create a new service instance
* Provide ActiveMQ uri and the extended class name
**/
$client = new Client('tcp://localhost:61613?tcpNoDelay=false', 'test');
/**
* New service reference
**/
$service = new ServiceProxy($client, 'ServiceName');
/* Service call */
$result = $service->get_clients();
get_clients() is a method that does the actual service call and it gets the DataSet structure in return. How can I manipulate this return value?
In .NET for example, there is a DataSet class. The ADO.NET DataSet contains one or more ADO.NET DataTables which in their turn consist out of one or more ADO.NET DataColumns and DataRows which are returned in a collection (array).
A simple code example, where the DataSet only contains one DataTable, can be:
/**
* Here, the val variable will contain the data positioned in the
* first field of the first DataRow of the first DataTable
**/
string val = dataset.Table[0].Rows[0].ItemArray[0];
I want to do the same in PHP, but I need a helping hand.
I would suggest you try learning more about the structure of the data you are working with. It is most likely some form of JSON or XML, which can be manipulated through the large set of PHP xml and json handling libraries.
Your first step is to look at the data being returned by your request and identifying it's format. Microsoft documents the ADO.NET API's datasets in depth HERE at the MSDN library. This should help you to make sense of what you are seeing when you inspect the data. Some data providers allow you to access the same data in different formats, depending on a parameter or a family of similar functions. Do you have documentation for your providers API?
Next, after you have identified the format and deciphered the specifics of the datasets schema, You need a class to manipulate the data. If you are dealing with something that conforms to a published standard you can use something like simpleXML, JSON or DOMXPath.This class should store the data in protected member variables and provide methods to inspect, iterate, load, refresh, search and so forth. You should refer to the PHP manual's function reference for help here and write whatever functions you need. I would write it generically to handle any similar dataset and derive a class to expose exactly the data I need for a particular application.
Another approach would be to write a COM component in a .net language to access and manipulate the data and import it's functions through PHP's COM extension. I think I would go with this choice if the data format is a weird proprietary Microsofty one.
The third possible approach works only if you have programming access to the server. If you can adapt the service provider to comply with the specification, Microsoft has released an interoperability toolkit which is supposed to act as a bridge between .net services and PHP clients using a layer of proxy objects to expose the ado.net data to PHP scripts. They throw the word RESTful around alot, but I'm not really sure what they mean by that. Check out the OData SDK for PHP HERE at Microsoft's interoperability HQ (there is info here about PHP and other Microsoft platforms and products like Azure, silverlight, Bing, etc., as well). As I said though, I think you would need the data provider to emit data which conforms to their standard. Perhaps it already does. I can't tell without a schema! If so then this is your best bet.
Good Luck!

Ruby equivalent to PHP's "unserialize" function?

I'm working with some serialized data from a MySQL database and I need to deserialize this using Ruby (the serialized data is used to build up a WHERE clause for a database query). PHP has the unserialize() method which will convert it into an Array; what is the Ruby equivalent of this?
The data in question looks like this, if it helps any:
a:2:{s:5:"Lists";a:1:{i:0;s:2:"11";}s:5:"Rules";a:1:{i:0;a:3:{s:4:"type";s:5:"group";s:9:"connector";s:3:"and";s:5:"rules";a:1:{i:0;a:3:{s:4:"type";s:4:"rule";s:9:"connector";s:3:"and";s:5:"rules";a:3:{s:8:"ruleName";s:2:"21";s:12:"ruleOperator";s:10:"isnotempty";s:10:"ruleValues";a:1:{i:0;s:0:"";}}}}}}}
I guess the exact equivalent would be this. You could also check out Ruby's Marshall Class, specifically Marshal.load.
Look at Ruby's Marshal Class.
From the docs:
The marshaling library converts
collections of Ruby objects into a
byte stream, allowing them to be
stored outside the currently active
script. This data may subsequently be
read and the original objects
reconstituted. Marshaled data has
major and minor version numbers stored
along with the object information.
Of course this is a two way street, you can only un-Marshal, Marshaled ruby objects.
If it's XML, there's the Hash.from_xml method.

AJAX - PHP Communication patterns

I'm building a webapp in MySQL/PHP/Javascript.
In PHP, I've got all the classes from the domain od the problem which persist in the database.
In Javascript, there is a cache with objects from the last queries.
When an object in Javascript is updated by the user, it has to update itself in server-side. Which would be the best way to do this?
Should I create a class in PHP and Javascript for communication purposes? Should each object in Javascript send an AJAX query to a different php file depending on the class of the object it needs to update?
Are there any patterns to follow to solve this?
Creating a separate PHP file for each class would certainly be more maintainable if this is a project of any size at all. It would also allow you to do things like have different server-level authentication based on the classes.
On the JavaScript side, you definitely want some sort of AJAX library, whether you throw it together yourself (I did one once in about 50 lines of JavaScript) or use one of the ones out there. You may need a helper function or two that knows how to serialize the data (XML, JSON, delimited, whatever).
You can write object oriented code in JavaScript, and if you're doing that already, it makes sense to add a write() or updateServer() method to call the AJAX library with the right parameters for consistency. If you're not writing OO code, it still may make sense to have separate functions, but only if there's more than one place you need to persist these objects from.
Most AJAX frameworks (jQuery etc) will send an 'HTTP_X_REQUESTED_WITH' header set to 'xmlhttprequest'. I like to use this to decide which view to use.
This means the same url can be used to retrieve JSON, XML, or HTML snippet via JavaScript or to return a full document if a standard GET / POST request is made.
This means your app will simply revert to normal requests should the user have JS disabled.
I think you should have a look into the RESTful API with PHP and JavaScript. You address your domain model objects as unique resources (e.g. /application/books/1). If you only want to implement CRUD functionality a generic Controller that updates the corresponding domain model (e.g using an ORM tool like Doctrine) should be sufficient.
If you really want to have the same model on the client side in JavaScript depends on your Application. I like the idea of just managing a single JavaScript object on the client side which will be loaded via REST and then populated to HTML Forms and send back e.g. as JSON (or as a simple form submit) to the server. If the client side model idea appeals to you, I recommend to have a look at JavaScript MVC which has a quite interesting model implementation.

Categories