PHP functions from Android - What's the best method? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I was wondering what is the most widely used method for calling PHP functions from within an Android app? The method I was thinking of doing was using a variable in the URL and retrieve it using $_GET from my 'functions.php' and deciding what function to run that way. I just wanted to know if this is the best way of doing it or is there another way people typically accomplish this?
Thanks!

The best way to do this is creating an "API" on your PHP website or app, at this point, you could call an URL using your Android app on the web app, which would execute the method requested.
For example, you could call an URL like this:
www.mywebapp.com/api/call?method=executeCrons&param1=1
Then you'd have in your PHP code something like this:
if ($_REQUEST['method'] == 'executeCrons')
{
executeCrons($_REQUEST['param1']);
}
It's simplified and you may want to add more validations and safeties, but you get the point.
You can also make these functions return a JSON string if you require a result.
There are other ways, such as using eval, but as mentioned already it can be unsafe.

I would take a look at SLIM (http://www.slimframework.com/) for PHP, it makes it super quick to set up a reasonably versatile, secure PHP REST API. I tend to use it to throw together server side pieces because it allows me to change implementation very quickly.

Related

Website/Files organisation for Ajax calls [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am just getting started with Ajax, and I wonder how to organize my ajax scripts and php files (which process the ajax function).
With Php it was easy, one functions library functions.php with all my classes and methods that I can call whenever I need them.
My first reflex with Ajax was trying to call an already existing method inside a php class in order to process a contact form but I do believe this is impossible and I need a separate file to do this ?
My question is, do I need to create a different file.js for each ajax script and a file.php for each ajax call ? It can get very messy with a few ajax functions compared to php.
Is there a right way to organize this ?
You definitely should use a php framework !
It will help you to organize your code and will bring to you some helpful features like routing which would solve your problem.
You will define some roots, each linked to a specific controller and your code will be well organized.
I can recommend you Symfony2 :
but there are a lot of which are able to do the job :
http://www.hongkiat.com/blog/best-php-frameworks/
If your application is small you can have a look to silex which is a light version of Symfony
To define your urls, you can use a REST api which is a best practice.
I can understand that you find the learning curve of the frameworks difficult but you will not regret it.

Most simple way to load content and keep it on the phone from the web? (PhoneGap) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I created an app with PhoneGap but I need to load some stuff from another domain. I tried jQuery load with a xdomainload plugin but It did not work.
The stuff I need to show run on PHP as the back-end language. I'm really noob - I've seen a few tutorials about JSON but all of them did not work and I ended giving up. Is JSON the easiest way?
Thank you all
Generally speaking, PHP wont run client-side in Cordova. If you're trying to access some sort of backend service that you built using PHP, then that's doable with JSON. Whether it's "the easiest", makes this question to open for interpretation. Generally speaking, a common approach may be to use javascript to communicate with backend services which "talk" JSON between each other. This question might get closed for being to opinion based. But that's my 2 cents.

PHP Ajax right structure [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I found a similar questions, but it's not exactly what I am looking for.
I write web site using php. For dynamic content I use jQuery Ajax.
I have 20-50 functions and I want to use Ajax to call these functions and take JSON.
Idea is to pass parametres via ajax POST. Pages works with database and job is done.
The question - how better organize it?
Should I create 50 separate pages like:
mysite.com/ajax/delete_project.php
mysite.com/ajax/delete_user.php
mysite.com/ajax/show_user_info.php
mysite.com/ajax/show_my_messages.php
mysite.com/ajax/show_my_tasks.php
mysite.com/ajax/send_message.php
.......
or create one page? or maybe i am completely wrong with all of that
Ideally you should be using a framework, such as Symfony. Otherwise, I usually keep all the functions for each content type in a single file. So you might have:
mysite.com/api/blog_posts.php which would implement GET, POST, PUT, DEL, etc... for all the blog posts. Meanwhile, mysite.com/api/messages.php would handle that for all messages.
You must start using an MVC framework if not using already. I would say Laravel should be a good choice, it's easy to learn, feature-rich and fun to work with. Start looking into Laravel Routes.
Moving to MVC is the best way to start getting into shape in your case.

ASP.NET and PHP on the same webpage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm wondering if there would be any problems using PHP on an ASP file? I plan to develop a website where I'll be inserting PHP code to load some info from a database but i'll be using ASP for the layout of the site.
Thank you in advance!
This is actually quite possible... if completely boneheaded, in this author's opinion, but it's a fun challenge. Here's how you'd do it:
If you'd like to use PHP to process the file initially, and then use ASP.NET, I would begin by using a VirtualPathProvider which uses a HttpClient (if .NET 4.5+) or HttpWebRequest to make the request to the resource that will be processed by PHP, the VirtualPathProvider is a means of making ASP.NET use a virtual filesystem, it's how you can store .aspx and .ascx files in a database, for example.
If you'd like ASP.NET to process the file initially and then use PHP, it's a bit more difficult, but still doable: you'd use ASP.NET as usual, but implement an IHttpModule which intercepts the result from ASP.NET and provides it as stdin to the PHP CGI executable, and then returns the result.
Implementing both proposals is an exercise left to the reader.

php web server and HTML [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have inherited a php site that is returning HTML pages. I always thought that the server returns data to the client and the client decides how to show the results.
Even though this is working, is this not a very tight coupling between the server and the client?
I would have thought a much better way to handle this is for the client code, javascript or gwt or what have you to ask for the needed data and the server returning that data only such as JSON object or a similar thing.
Thoughts on this?
It sounds like you could benefit from making an AJAX call (via js) to a php page, then manipulating the data (JSON object, string of comma delimited data, raw HTML, etc.) returned on the client side.
Sorry for the bad previous example, this example is a more sophisticated, modern example of how an ajax call should be made.
It appears that it is not at all uncommon for php scripts to return HTML. It does create tight coupling with the client application. Returning JSON does create a more loosely coupling with the client.

Categories