Call PHP code from Python - php

I'm trying to integrate an old PHP ad management system into a (Django) Python-based web application. The PHP and the Python code are both installed on the same hosts, PHP is executed by mod_php5 and Python through mod_wsgi, usually.
Now I wonder what's the best way to call this PHP ad management code from within my Python code in a most efficient manner (the ad management code has to be called multiple times for each page)?
The solutions I came up with so far, are the following:
Write SOAP interface in PHP for the ad management code and write a SOAP client in Python which then calls the appropriate functions.
The problem I see is, that will slow down the execution of the Python code considerably, since for each page served, multiple SOAP client requests are necessary in the background.
Call the PHP code through os.execvp() or subprocess.Popen() using PHP command line interface.
The problem here is that the PHP code makes use of the Apache environment ($_SERVER vars and other superglobals). I'm not sure if this can be simulated correctly.
Rewrite the ad management code in Python.
This will probably be the last resort. This ad management code just runs and runs, and there is no one remaining who wrote a piece of code for this :) I'd be quite afraid to do this ;)
Any other ideas or hints how this can be done?
Thanks.

How about using AJAX from the browser to load the ads?
For instance (using JQuery):
$(document).ready(function() { $("#apageelement").load("/phpapp/getads.php"); })
This allows you to keep you app almost completely separate from the PHP app.

Best solution is to use server side includes. Most webservers support this.
For example this is how it would be done in nginx:
<!--# include virtual="http://localhost:8080/phpapp/getads.php" -->
Your webserver would then dynamically request from your php backend, and insert it into the response that goes to the client. No javascript necessary, and entirely transparent.
You could also use a borderless <iframe>

I've done this in the past by serving the PHP portions directly via Apache. You could either put them in with your media files, (/site_media/php/) or if you prefer to use something more lightweight for your media server (like lighttpd), you can set up another portion of the site that goes through apache with PHP enabled.
From there, you can either take the ajax route in your templates, or you can load the PHP from your views using urllib(2) or httplib(2). Better yet, wrap the urllib2 call in a templatetag, and call that in your templates.

Related

How can I use Angular 2 in PHP application?

I am a PHP developer and I started learning Angular2. But I don't know how to use it with PHP.
Is it possible using Angular just as frontend?
What I must to do?
How to use it on shared hosting without Node.js installed?
My 2 cents as a long time PHP developer who has been playing with Angular2 quite a bit.
As a PHP developer, you're expecting to have your PHP render an HTML page and send to the client. You won't be doing that with Angular2. All that processing that would take place in PHP, building data tables, lists, or whatever is now Angular's job.
The only thing you're gonna do with PHP now is simply send JSON responses. Others have said this already above.
I'm assuming that since you're asking this you have little to no experience working with Angular2. So here's the deal:
Learn how to use Node and NPM on your local machine. Learn how to use NPM to set up your empty Angular2 project. Play and develop on your local machine.
When your ready to load in data, PHP can get involved by sending JSON data down to the front end for Angular to use.
When you're ready to put your Angular2 app online for the world you have a number of build options. You need to compile your code from Typescript to Javascript.
I've been using the Angular CLI tool. That lets me just run "ng build" and the app gets compiled.
Then I can upload the folder it generates up to my apache server and it works. The Angular CLI makes a folder called "dist" that contains all the stuff your front end will need.
Piece of cake.
Preferably, your site will just download a simple index.html and a file called app.js which contains all of your JS and therefore your Angular app.
PHP will be sitting on a server doing the job of an API, which is answering with JSON/XML to request, you angular app will then use the JSON to build the web interface.
You can have PHP hosted anywhere, and serve your angular app from another place, even thought it's not recommended because of latency
<html>
<script src="app.js">
</html>
as far as I know AngularJS is client side Javascript framework. So in general there's no need for 'nodejs'. You just need the AngularJS library files included in your HTML that's going to be produced by your PHP code.
I guess you should have a closer look at AngularJS at first.
above [sitesbyjoe] best answer is awesome.
Just details steps,
open cmd
1) cd your-project folder
2) ng build
3) copy dist folder to apache/htdocs/[dist/or your-project-name]
4) Important: open the index.html file, find <base href="/">
change it to <base href="/your-project-name/">
Without doing this, js file will not be load correctly.
5) http://localhost:80/your-project-name/index.html
It works.

Aurelia + Php Possible/Recommended?

Before this question gets closed, I know the setup above is possible. I just want clarification on some things.
I just started learning Aurelia because I want to convert one of my projects into a web app. My project is built with html+css+JavaScript(jQuery)+ PHP(MySql).
I havent used any sort of framework before.
In the guide, they mention a few ways to setup a web server. I used the http server with node. Now this is where I need some help understanding a few things.
I dont want to use node.js. I want to use PHP on the server. Will that work and how?
When using Apache server, I know any PHP page is sent to the interpreter that renders the final html. I use XAMPP and its apache comes bundled with PHP. Does the http server used by node come with PHP? Is this even a sensible question?
Now I know Aurelia is purely front end. If it used to make single page applications, it uses Ajax. So now I made the following assumption:
Using Aurelia, the user accesses the root page of the app that the web server sends. After that, Aurelia makes various Ajax requests to the server which will use my PHP files to do database query stuff.
Is that right or am I missing something. And can I just use xampp(apache) to host my app instead of server from node?
Aurelia is a framework that, after you export it to any server, does not rely on any back-end software at all. This means that with the help of the http- / fetch-client API, you can just call out to your php script.
I have an example in my github:
https://github.com/rjpvroegop/randyvroegop.nl-made-with-aurelia
Here I use the http-client to post data to my php script wich has a very simple email functionality.
You can see the action inside my view-model in src/pages/contact/index.js.
You can see the PHP script in src/assets/components/contactengine.php.
These work the way they should. Note: you have to change your gulp build if you want your PHP served the way I serve mine, from the dist folder after gulp-watch or gulp-export.
Next to that you can use any back-end functionality you would like, as long as it returns the proper data. This PHP script does that. If you would download my distribution to test this you can simply do the following:
gulp export from your terminal in the root folder
copy everything from the export folder to your PHP webserver.

file_get_contents/curl blocks other clients

I use file_get_contents/curl to get access for one API at the another server from my php script. This API isn't fast and can take up to 10 seconds to respond.
When I try to open 2 pages on my web site at the same time, which uses this API, they loaded one by one, i.e. I need to wait 1st to be loaded before server will start to server request for 2nd page.
I use Apache2 and php under linux.
How I can avoid such behaviour, I don't want to block other clients while one of them access this API. Need help!
Thanks.
Yes.
There is this PHP library: http://code.google.com/p/multirequest/ (it's a multithreaded CURL lib).
As another solution, you could write a script that does that in a language that supports threading, like Ruby or Python. Then, just call the script with PHP. Seems rather simple.

GUI That Runs PHP Files

I have created a html page which sends custom data to a php file which then processes and evaluates it.
My next task is to make this into a GUI with the requirements:
1. A box for a custom search with button (it then posts this into the
php)
2. A box where xml/json request can be seen
3. A box where the xml/json response can be seen
4. A box where the parsed version is translated and made to look pretty.
***MUST CONNECT TO INTERNET, PHP ESTABLISHES CONNECTION BUT DO NOT WANT A GUI ISSUE
Any suggestions on programs or languages etc which can help me communicate with PHP in GUI form. It needs to be able to access the internet!
I was thinking perhaps Visual Basic as that's the only one I've ever used that really uses GUI's but I'm wondering what you all think!
Thanks!
Basically, what you're asking for is a web browser, with a very simple little HTML/Javascript front-end web page to make the PHP calls and display the results. I'm not entirely sure what it is about a browser environment that makes you think it's unsuitable, but it's basically exactly what you're asking for.
If a full-blown web browser really isn't suitable, you could try using a web browser control inside a simple GUI app. This would still work exactly the same, but would be without the browser controls, such as the URL bar.
Just use a browser.
If you don't want to do that -- build a browser.
If you are just looking for basically a web-based REST testing tool, try the Firefox RESTClient plug-in.
Why don't you use a framework ?
You may take a look here:
AppJS for Linux, Windows and Mac using HTML, CSS and Javascript
Adobe AIR : cross-platform using ActionScript/FLEX or HTML/Javascript
Titanium : HTML/CSS (no support anymore)
PhoneGap : mainly used for cross-phone-platform, but here's an Windows implementation of it (you should read the README.md ...)
You may also check this from Mozilla

AppMobi / Phonegap Alternative with PHP

Not really a coding question exactly, and not sure which stack site to throw this on, so here goes.
Im wondering if there is any framework similar to appmobi/phonegap in the sense everything is sandboxed and compiled into an app format for both iphone and droids. Where the sandboxed server comes with the ability to run PHP on it, I've been tryin to search all day and I've come up with nothing as of yet other then heaps of articles on appmobi/phonegap. This could be a free or paid for framework (preferably free/open source).
If theres no frameworks like that then is there a means of taking something like apache itself that acts and runs like a webserver where I can load php into it, and at the end of my project compile it into one final package for use in the mobile markets?
The problem is that iOS generally uses Objective-C and the Android uses Java, but both can use javascript, which is why phonegap works.
So, unless you can write two webservers, one for iOS and one for Android, and port PHP to run under your webserver, or, more likely, write an interpreter that will take PHP and transform it to run on your webserver, then it would work.
But, if you write a mobile web application and use PHP as the code on the server, then you can send javascript and have that run in a browser on both devices.
The best option would be to have most of your business logic be in REST services that are written in PHP. You can do this in PhoneGap by following this blog:
http://share.ez.no/blogs/thiago-campos-viana/rest-api-basic-http-auth-and-phonegap-using-jquery
Here is the main part of the article though, so you can see how easy it can be.
//10.0.2.2:80 is the localhost in android emulator, app needs internet access
$.get("http://10.0.2.2:80/ez_site/api/ezp/content/node/2",
function(data) {
//code goes here
});
By doing this then you can have most of your business logic in PHP, and have just the UI be in javascript, able to take advantage of the hardware on the phones.
No. Attempting to include a web server and PHP runtime inside of a mobile application would have serious performance implications.
The reason PhoneGap works is because Javascript can execute natively in mobile applications by using the built-in web browser's rendering engine.

Categories