Python to POST data to local PHP on linux - php

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&param2=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.

Related

Running PHP WebSocket on Ubuntu

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?

Show windows application in a php file

I have searched everywhere for the answer but cant find any reference anywhere
i am building a local database and like to include a page that shows a piece of software that will be installed on windows is there away so show this such as a iframe.
sorry if this question is very vague but i am not sure what this would be called.
There will be no interaction within php to the application and the server is ran of the same windows machine.
it is not possible.
what you can do, and only if the software supports it, is to send arguments by the command line using exec function and show the user the response.
exec("path/to/software.exe -argA -argB",$result,$error);
var_dump($result); //<--the software response.

Getting started with solr

I've struggling a lot trying to get solr working. I've read the official docs and tutorials, tried them out but still couldn't figure out how to get started. I took to randomly test out blogs which resulted in crashing of my ubuntu OS.
So, I'm framing a question here, outlining every possible doubt of mine and everything I've tried out.
I downloaded the apache solr 4.3.0 from here: http://lucene.apache.org/solr/
Next, I unzip the file [path: /home/ps/solr] and inside there is an example directory containing the schema.xml, solrconfig.xml and other important files.
Next, I choose the official solr php client and installed it. [Link: http://docs.php.net/manual/en/book.solr.php]. It requires a PECL extension which I forked from here [https://github.com/lukaszkujawa/php-pecl-solr] as there are some issues with running it with solr4.x. The path of php-pecl-solr is home/ps/php-pecl-solr. I followed this blog (http://systemsarchitect.net/integrate-php-application-with-solr-search-engine/) to install the PECL extension.
After all this, I tried writing down a test script (http://dpaste.com/1173282/) as in the blog, and running it: it displayed nothing. The page wasn't opening. The file's path was var/www/testsolr/index.php
My doubts:
Why isn't the page opening? What am I doing wrong here?
How to connect the example directory of solr with this php-pecl-solr extension?
I'm just a beginner with these things and would be happy if someone helps me out.
Solr comes with logging code, but it's not enabled by default.
From the example directory, rather than "java -jar start.jar", add a system property on the command line that imports the logging.properties file. If you launch start.jar from the example directory, use:
java -Djava.util.logging.config.file=etc/logging.properties -jar start.jar
your log will be in example/logs/solr.log.
I recommend that you not use PHP until you've got solr properly configured and running reliably, after you've been able to import your data and you can do queries from your browser the return the expected results.
I'm new to solr too. I've been beating my head against it for two weeks, but I'm starting to have some success.
Your log is likely to contain lots of Java exception stack traces. These are almost always the result of errors in your configuration files, but the log is very unhelpful in that it doesn't tell you what line of which file has the error! Thus I've had to beg Google for insight, which is sometimes found here at Stackoverflow.
Make sure you have your JDBC connector installed. It will be a free download for whichever database server your using, but is not bundled with solr.
First you need to make sure you have Solr running on your server. The simplest way to this is to run java -jar start.jar from the example directory under /home/ps/solr. This will startup Solr using Jetty at the url of http://127.0.0.1:8983/solr.
You will need to modify your test.php file and put the full url http://127.0.0.1:8983/solr for your hostname item in your $options array.

PHP -> python seems to not work on web server

I have a few pages (one html, one php, and one python script) that takes a user input, and then outputs a pdf file (using reportlab) for the user. When I test on apache (localhost), it works perfectly.
However, online on a real web host it is not working. The version of PHP on the web server is 5.2.17 and python is installed.
Could the version be an issue? I am very lost because I'm not very experienced and it works perfectly on Apache and not at all on the internet.
Last thing, the command I use to call the python function from php is this:
$ed = exec("python pdfgeneration2.py $name $age");
I also thought maybe there is a better command for this?
My question may be vague and unclear but if anyone has any ideas it would be greatly appreciated.
Look at /var/logs/httpd/error_log, or wherever your distro's apache stores logs. Odds are you'll find an error message there from PHP.
In addition to checking the path like sberry recommended, double check perms, too. Apache will need execute permissions on the python script: chown apache:apache pdfgeneration2.py

PHP on windows environment Vista

Do I need a special library to run PHP on Windows environment Vista?
I can write simple message like echo "hello" but whenever I try to run any these scripts no error nothing display on the page http://php.net/manual/en/function.crypt.php.
I'm new to PHP please help
When in doubt...
<?php echo phpinfo(); ?>
If that code outputs information regarding your PHP version and settings, PHP is installed correctly.
There would be errors in your code if nothing is displaying. Post the code you are having trouble with and we can help you out.
Have you actually set up a local server? You'll need this to run PHP scripts. For a newbie I'd recommend WampServer since it's really easy and handles all the complexity of installing a server. Once installed and running, try http://localhost/ in your web browser.
Based on your comment though, I think the problem is that the sample code on the page you posted won't work on their own, you'd need to combine them with other things. For example the first one references a variable $user_input which you'd replace with a variable taken from user input (eg a form) on your site.
I suggest finding some tutorials online (or buy a book) and walk through various proper examples to familiarise yourself with PHP.
Do you have PHP / Apache installed at the moment? If you don't, try XAMPP. It comes with an easy and convenient installer.
Like most languages your computer must have the language/compiler installed in order develop and run scripts/applications. PHP was originally designed for web development so you won't usually find it installed on PCs by default.
The easiest way to develop PHP is to upload the scripts to a web server that has PHP installed and then test in your browser in the same fashion that you'd test an HTML page.
Otherwise to do it locally on your computer you can install a web development environment which acts like a web server. Essentially eliminating the upload step.
I believe the most popular for Windows is Wamp Server

Categories