I am learning to build a web application based on graph theory(Linked-In) kinda thing. So as I thought of considering PHP for getting the inputs from user via web page and using C for internal programming like adding,deleting vertex, edge to/from the graph with the help of C structures and various algorithms. So I need to know how to use C code to drive my functions while I use ASP or PHP for handling webpage.
I wish to use C preferably.
Using Xampp 1.7.4 on Windows7
Actually it shouldn't be that hard to code...
If you already learned C, Then I suggest you to start learning sockets.
You didn't specify the OS, So here's links to a great tutorials both
Linux
&
Windows.
Now, if you don't want to use sockets, I suggest you to take a look at "Apache - Thrift".
You can communicate between functions in PHP and functions in C.
Example -
$shutdown->Shutdown();
Or
$points = (int*)points;
Good luck.
Related
I've been looking through the Predis code on GitHub and it's massive so it's a bit difficult for me figure this out from just looking at the code because I don't understand what each folder/file is for
What I'd like to know is, how does PHP actually contact the database when using MySQL or Redis or any other database system? Does it make system calls similar to how you can do in C with system("some command here"); or does the developer have to actually extend the compiler for the PHP language to do this?
You don't have to "extend the compiler", but you have to do what you probably meant by this: write a PHP module/extension in C that talks to the database's (typically) C library API. What does that mean? A database typically comes with connector libraries that are often written in low level C and thereby offer a C based API. That more or less works like an include 'api.php' which then allows you to call functions of whatever you just included, but it's specific to C. PHP code cannot talk to C code directly, but a PHP extension written in C can act as a "bridge" between PHP code and the C API.
That C library then has many options how it may talk to the actual database. It may talk directly to another C API of the database, though that's not necessarily typical. Often a UNIX socket or TCP socket is used, sometimes across the network if the database is on a different machine. You could be talking directly to that UNIX/TCP socket from your PHP code if you wanted to, but that means you'd have to reimplement the entire protocol to talk to the database in PHP code. That's typically inefficient, since PHP is a rather high level language and doesn't offer any direct access to raw computer resources like memory, which makes this implementation rather inefficient.
So, the way it typically goes is:
the database offers a protocol to talk to it over a socket of some kind
an official protocol client is implemented in a C library, because it's efficient and portable
someone writes a PHP extension to bridge that C library API into PHP userland code
There's nothing stopping you from implementing that protocol in other languages in alternative clients, but since this is often a tedious process and C is a widely used system, people typically end up writing wrappers around the existing official C library.
The functions/classes for interacting with the various databases are provided (like pretty much everything in PHP) by PHP extensions, e.g. the mysqli extension. They are .so files on Linux or .dlls on Windows. Or they can be compiled into PHP when it's built. Either way, they call functions in the Zend code to register the functions/classes that will be called in PHP (e.g. mysqli_connect) and hook them up to code that knows how to interact with the database. PHP and the extensions are written in C. If you look at examples of how to write C code that connects to MySQL, that's probably what the mysqli/mysql extensions in PHP use.
I'd like to get apache, mysql, and php running in an iphone app. This is because I'm finding objective c to be quite difficult, and if I can get those three running and figure out how to set up a full-page browser or something, I can code my app like that.
The MySQL aspect isn't really a necessity since I can very easily code a flat file database in php for this.
Is this possible? Has it been done? If not, what frameworks should I use to make creating an app easier?
I am assuming what you are saying is that you are more familiar with HTML and JavaScript, and would like to code with those tools.
If so, take a look at PhoneGap and Titanium. They are frameworks to code iOS apps with web technologies. Basically, it is like running a local file in a web browser (with interfaces to access device functions such as the GPS). No need to have a PHP and Apache stack there.
There is also a quasi-standard to write web applications that can work offline once downloaded. Mobile Safari supports this, and it can almost look like a native app.
If you're looking for any other options, there's Mono Touch, which is C# for iPhone. It's garbage collected, managed, strongly typed, and has access to all the device functions just like PhoneGap.
There's not really any simple way to get PHP running on an iPhone. You could try to get it to compile yourself, but that sounds like you'd be spending more time trying to get the framework working rather than working on your actual project.
I'm choosing between these two languages, in contrast to another question here. My program going to be set of scripts checking files, free space, checking that apps are running etc. and writing information to database. Then PHP web page will show results.
Python have more examples of winapi calls (at least at StackOverflow) and more attractive to learn and use in different areas of programming, but I didn't use it before. With PHP I have small experience and I will be using it for results web page anyway.
PHP seems to be easier to install on network (to use one network installation for many workstations). Both languages can work with COM objects (I will need ADO).
Scripts will be small, not complex, but their number will grow.
Can someone with experience in similar tasks advise on choice between these two?
Don't shrink from using a language just because you haven't used it before. Learning a new language is one of ways to expand your overall capabilities as a programmer. True, you won't be as productive with it at first but learning how the language tackles common problems (manipulation of data structures, creation and lifetime of objects, etc.) will teach you new ways to think about computing.
That said, I would suggest you use the ActiveState version of Python (http://www.activestate.com/). It has the Windows integration included. I'm not sure about network install with Python. I've installed it across a net by running a remote desktop of the target and running the installer. Windows wants things registered in the registry so running a language is not like running an .exe sitting on a remote file share.
I asked question first time as anonymous so have no other option to reply now except to write an answer.
Thank you verisimilidude for ActivePython advice!
Installed ActivePython at home, reading Dive into Python 3.
Installed RadPHP XE at work (company bought it).
So for work it will be PHP. For me, if have free time (and may be for kids, when grew and if interested) it will be Python.
As someone who uses both on a regular basis, I tend toward using python for windows scripting and php for web apps. It's mostly just my preference, but I think they're each ideally suited toward different things. I love using Python in windows, it is really versatile.
a ) python is a nice alternative because you can generate an executable, otherwise (running as script) php is more suitable because it does not required an installation.
b ) try to not to connect directly to the database. Instead, you can create a web page then the clients can connect to it instead to do a direct connection to the database.
for example in php (in the client)
$fp=fopen("http://192.168.0.200/update?id=mymachine&value=200","r");
fclose($fp);
ps: For api calls, both (php and python) can do the same. Thought, for php and Windows Vista (and higher) exist some restriction when you are running it as a apache-module and apache is running as a service but i don't think it is the case.
I'm going to start a new project with instant message support.
I find that there is no good long polling solution in PHP, but there is some good ones in Java EE.
I'm wondering if I can integrate PHP and Java EE to get the function? Or should I just use Java EE instead of PHP?
Keeping the number of development platforms to a minimum is always a good idea. It will keep deployment requirements low, gives less operational complexity and gives the best possible integration.
If you want to replace PHP by Java, you have lots of frameworks you can use to replace PHP. A good option would be the Stripes framework it’s an easy to use MVC framework that does not need much configuration.
An other very workable solution would be using Java in the backend for filling the database. And use PHP in the frond-end and use the database to retrieve data. This way the integration is limited to a shared database.
If you would like to have a direct integration between PHP and Java, things start to get more complex. It could be implemented by either web services or the faster php-java-bridge. But I don’t think you need this and if you do, I would seriously consider migrating the whole project to Java.
For an implementation example of the php-java-bridge see: How to share session between Java and PHP
You CAN build your application with PHP together with Java EE. The PHP is to generate HTML, and Java EE supports Comet.
Actually, Facebook generates HTML with Apache server. I bet Facebook web pages are written in PHP. And the Comet server of chatting is written in ErLang hosted in Mochiweb. Two languages & servers works fine together.
The decision is up to your own preference.
Is it possible to access & control external devices in PHP?
For example, would it be capable of changing the speed of a USB fan or change the direction of a wireless toy car.
I'm trying to go beyond web dev and I'm wondering if the programming language I use is capable of handling my ideas or should I consider changing the environment.
If these would be possible, I'd very much appreciate any pointers to reading materials or suggestions on other languages that might be more suitable.
Thanks!
On Linux it surely is possible by accessing /dev/ files. But it'll be very tedious. I'd recommend you switching to Python, Ruby, Lua or Java.
For example there are bindings for libusb for Python, Ruby, Lua and Java.
You could write an external program, then use PHP's exec (or was it system?) function to interact with the executable or script.
Seems like the most sane way to do it. Another good alternative is to build a program or script that controls an external device that can communicate with a RESTfull type API exposed via HTTP - and then use lib_curl in PHP land to talk back and forth between it. Believe me, building a basic HTTP server in C++ that can be used to be remote controlled with PHP (or JS for that matter) is very simple.
Wait
I think I read the question wrong ;)
If you want to get into really cool stuff, I say that you learn C++. C++ is a great language that not only opens a lot of doors, but also provides a good learning experience. C++ is lots and lots of fun.
In response to comment
In the case with USB its a bit different and more complicated (as USB has an established protocol and such) but serial is as easy as dumping data into a handle.
You should be able to pick up C++ to get to that point fairly soon. Either way it's a great experience.
This would be possible with a extension, but not with pure PHP code. I don't know of any extensions being able to do something like this, but I think it should be possible.
If you find a command line tool that does this, then you can control it using exec(), system(), passthru() or shell_exec() (based on what output the program gives you back)
Just be sure to escapeshellcmd() if you give access to this program from a public website.