Introduction
Before getting started with the actual question, I’d like to make a notice of being an extreme beginner to the world of web sockets using PHP. As a matter of fact, I’m still trying to grasp the very basics of it. I do however have lots of experience with sockets overall, from languages such as Java.
Upon doing my research, I stumbled upon the tutorial from PHP builder, whereas I think myself to having learned how to set socket connections up using this simple library.
Problem
Unto the problem we go. For a development server, I have chosen to go with XAMPP—it’ss sort of stayed with me from my Windows days, many years ago. At this very moment, I am using the terminal UI for XAMPP in Ubuntu—meaning, I launch the XAMPP server and then do a killall should I ever wish to close it.
In the tutorial mentioned above, you enter this information into your PHP document where you keep your server socket:
$server = new Server('192.168.0.8', '8000');
The Server class being a class extended by the WebSocketServer provided in the aforementioned library. You’re then supposed to run the PHP server socket using (with the dollar sign symbolizing terminal input):
$ php -q serversocket.php
It was suggested somewhere else on Stackoverflow that you run the server socket with the -f flag instead, but both of them—instead of starting the server socket—prints out the file contents in the terminal.
The PHP I’m using (to attempt running the server socket) is the one you get from running (on Ubuntu):
$ sudo apt-get install php
I’d assume this to be the regular version of PHP.
I’m honestly probably just being a huge moron, but truly do need help to solve this problem. Have I missed something?
The problem that was occurring at the time was the PHP code being displayed as raw data in the terminal whenever run. I was using short opening tags at the time <? and did have them enabled in the configuration.
I did however, resolve the issue, by rewriting the code to use the full opening PHP tags <?php. Perhaps there was more things to configure, maybe the version of PHP I was using was broken. Who knows?
Related
So I am fairly new to PHP and websockets and server management in general. But have been tasked with designing a web-app and have determined that the best way to implement this is with websockets. So I found ratchet. And began trying to get it to work. I have a linuxbox with apache already setup, and created a new directory in the webroot and began Ratchet's tutorial. However no matter what I did I could not get even the in-line telnet part working.
I have a composer.phar, I have their script within my composer.json, I followed their instructions for installing ratchet. however when I run the php chat_server.php command it doesn't seem to do anything. The tutorial states that it should take control of the console, and as I saw in a video tutorial it does.
So my ultimate question is - what is causing this not to run properly? Is it a bad installation (Did I mess up installing somewhere or just not install something that is required but was not explicitly stated?) all the code is identical to what is documented here http://socketo.me/docs/hello-world
Some questions when ratchet is not functioning while setting it up.
Is the php process (#php chat_server.php) open?
Are you telnetting from another cli?
can you check if the port on which the chat server should be running is allocated?(#netstat -pln)
Is the zmq library added to the apache modules? (Probably your problem)
If the above aren't working you might try to check if some firewall is blocking the connection internally.
I would suggest starting from a clean machine to execute all set up steps.
I recently started PHP (I am a beginner) and I wanted to create a form with HTML to add numbers together. However, when I send queries to PHP it shows my source code (because it is not a server - file is run locally). I was wondering would it be possible to preview my PHP's code result on a website of some sort or even better on my computer? Do you know any services or ways to accomplish this, preferably free of charge?
To run php files online, go to Write Code Online.
As long as you can install programs on your computer, you should install a web server to run php files locally. To run any basic app, especially if it uses forms, you will use multiple php files and the testing will be too complex to run online. If you are using windows, install wamp. For mac, try mamp.
you seriously require some help :-)
First, PHP is a server side scripting language, so it requires server (like Apache, which you can install using the wamp or xampp installers) Try to search google on how to install them on local computer.
Second, their are some online PHP editors, like http://www.compileonline.com/execute_php_online.php or writecodeonline .com and many others. It may not replace the server but helps to write the PHP code and execute it for the sake of practising.
Also, it is better if you read some tutorials about PHP. One tutorial I can point you to is the http://www.w3schools.com/php/php_form_complete.asp which is related to question.
This may sound a bit strange but I need this to happen like this for a reason.
I need python to be able to post data to a local PHP file on a linux server and then get a response from the PHP.
I have tried this:
p = subprocess.Popen("curl --data 'param1=value1¶m2=value2' /home/hbmukwm/temp/receive.php", shell=True, stdout=subprocess.PIPE)
reply = p.stdout.read()
But when I do this, as it's a local file I get this curl error: curl: (3) malformed
I also tried it this way (found on a stackoverflow answer)
mydata=[('one','1'),('two','2')]
mydata=urllib.urlencode(mydata)
path='file:/home/hbmukwm/temp/receive.php'
req=urllib2.Request(path, mydata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
reply=urllib2.urlopen(req).read()
But in this case I get the php response as plain text like I'm just trying to read the PHP file, again I think this is happening because the PHP file is local, like this:
<?php echo "test"; ?>
Rather than just
test
Hope this is making sense so far. All I need to do is POST the data to the local PHP file, and then get the PHP response back to python... Is this possible...
Any response is highly appreciated, sorry for my ignorance!
Thanks everyone for your responses, I have learnt something at least! The reason I wanted to have it as a local file is because I didn't want the PHP files to be publicly accessible, though data needed to be sent to them from any location the application was run. So my idea was to create a small python application that runs on a port on the linux server that takes commands and passes them to the PHP files local to the server.
Anyway, the way I have got this working is by using a command line (Like my first example) except using:
p = subprocess.Popen("php /path/to/file 'data=this'", shell=True, stdout=subprocess.PIPE)
And then in the PHP files grabbing the argument like this
$data = $argv[1];
This isn't posting the data to the PHP files like I initially was asking for but I guess I mis-understood what I needed to do. So thank you for all your help guys! I can't really figure out which one of your answers was the definitive answer because you were all correct in what you said, but the answer I am posting now is how I overcome the problem.
Thanks again guys, Stack Overflow is the best :)
You need a web server to run PHP code. You can't just refer to it as a filesystem location. I recommend that you install [WML]AMP web server bundle on your machine which will install Apache, PHP, MySQL and it's easy to setup and configure for beginners. Here are the links for the AMP family. Use the one that
Windows : WAMP
Linux: LAMP
MacOSX: MAMP
You will need a web server to actually parse and interpret the PHP scripts. However, in case you want to do it via the command line this page should give you some details:PHP Command line usage. You have not mentioned which version of PHP you are using, however this appears to be supported since PHP 4.3.0
I guess it is a matter of taste, but I am using "xammp" from www.apachefriends.org
You can easily setup a webserver from there with php5, sql and so on. Wit BitNami you are also able to easily install joomla and other stuff.
The short version of the story is that when I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
The longer version:
Trying to learn web programming concepts. So far I've done a lot of work with PHP and MySQL using MAMP. I have created a big Javascript/PHP app and a big MySQL db to go with it. Now I'd like to learn Python, and I'd like to start with a familiar environment, namely, my MAMP setup and the db I've created.
I run my PHP scripts by entering localhost:8888/the-script.php in my browser. Now I have python scripts that run the same way, so I know python is working in my MAMP environment. What I haven't figured out yet is how to get my python scripts to talk to the MySQL db.
I installed Django, and I got it to see my db, but it wants to run its own server. I don't want that. I want my python scripts to run on the same server as my PHP scripts (I suppose I could try to get my PHP scripts to run on the Django server, but I'd rather consider that a last resort, to be taken only in desperation).
I've tried installing various packages with pip and macports, and I can run python from the command line and import MySQLdb, and it works fine. I've been all over stackoverflow and tried many of the applicable suggestions. I found a straightforward set of installation instructions that I thought might be the answer. But if I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
I was hoping to learn python without first having to get a PhD in a lot of other areas like how Apache works internally, rebuilding MAMP and MySQL from the source code, etc. I realize that learning web programming concepts will eventually include those things, but I'm not ready for that. Right now I just want to learn python.
Is there a simple way to get a python script to run in the same environment as my PHP scripts, and interact with the same MySQL db that I've created for use with my PHP scripts?
I've just started reading about php, it needs me to install php, apache and MySql to run any php script. can any one suggest me a simplest method to install php, apache and MySql so that i can sun those php script.
I've tried the zip files from php.net, Are those required to run the script offline for practicing the script? what do i do with them? i felt the things given on the same site a bit complex, and was unable to run the script. please help.
I'm assuming you're using Windows - get wamp - it has exactly what you need:
Apache
MySQL
PHP
Wamp comes with a nifty tray controller app which you can use to access the most common tasks, like restarting services, editing config, etc.
Equivalent to Artem's post, just a different organization which packages it differently. And my preference. It is called XAMPP.
If you are using Windows, you could install XAMPP to have the environment setup for you. You'll then need to learn how to bring up the server (basically executing the XAMPP control panel program), copy your PHP scripts into the correct directories (for XAMPP, it'd be C:/[xampp installation dir]/htdocs), access MySQL and creating the necessary MySQL databases (you could access the MySQL via the included PhpMyAdmin).
You can probably refer to a tutorial here (it's a YouTube video, BTW).
Don't have enough rep to comment but wanted to add something to Artem's recommendation of Wampserver. It's what I use on my windows machine, and one of the nicest things about it I find is the ability to have multiple versions of versions of apache, php and mysql installed alongside each other as plug-ins and then select which to have running at any time. It makes it easy for me to emulate the eventual hosting environment for any given project.