Run php script remotely - php

I have a php script (qa.php) that my app points to on my current server. I just recently switched servers and want the qa.php script running from this new server.
Is there a way to have a php script redirect or automatically run the remote php script?

If you mean a redirect in the sense of a client visiting this page, you can use the header() function to change the location header and simply perform the redirect.
header('Location:http://www.your-new-domain.com/qa.php');
But if you mean (and this I'm assuming) running the script without direct client interaction, there are a few different approaches. One good way is to use the Curl library to send a request formed the way you'd like to the script. I've used this method in scheduling cronjobs before that had to fire signals to several controllers at once and record the output.
http://www.php.net/manual/en/intro.curl.php
It's also acceptable to use the file_get_contents() php function to simulate a client visit on the page if you don't need to manipulate any specific headers for the request going to the server-side script.

Related

PHP post to url

I have a python script that listen to specific port on a server and waits until it gets an http request from browser or program to run.
I want that script to run when the admin on my Joomla site clicks on the save button.
That means that I need a way to go to the link that the server listens to without showing it to the the user (a way to do it in the background).
Any ideas?
Thanks!
PHP is server side, so anything implemented there would not be visible to your user, unless it gets rendered as HTML output.
Does the request need to come from the client machine? Or can it come from your web server?
If it needs to come from the client, I would recommend using PHP headers.
Script 1
header("Location: exec_something_private.php");
exec_something_private.php
exec("do something");
header("Location: success.php");
The above implementation could call a php file, do what you need to do, and then redirect them immediately to a new page?
If the function can be called by the webserver, simply uses exec to run a curl request or something, no?

PHP Execute Code AFTER user receives a response

I have a api built with PHP & Codeigniter + RestClient and its getting to the point speed matters. We have ALOT of code that the user should really not need to have to wait on to receive a response from the API that I would prefer to be executed AFTER the user receives a api response. Is this possible and if so how?
This is possible if your server run PHP as PHP-FPM. See in documentation functions with prefix fastcgi_
You can move your "special" code to separate file and run this file as daemon. Not sure about PHP scripts, but for Python scripts this is possible.

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.

How to have a listener PhP script respond to GET requests

I'm trying to accomplish remote php call and return as described in this question: PHP: Remote Function Call and returning the result?
However, I don't know how to make the listener script that responds to the GET request. How do I create the script, allow it to be called remotely, and have it process the request appropriately? Is there an example somewhere?
I understand this is probably a very low-level question, but I'm very new to PhP.
You don't need to implement a listener, the web server is doing that for you. Just create a script and put it in your web directory. When the web server receives a GET request to that script, it will execute the PHP script.
In the script, you can just echo the values that you want to send back in a format that your calling function understands. (Of course you could also set some headers, if you feel that this is necessary).
Your listener PHP script responding to a remote call should be just like a script responding to a web browser's request. Use $_GET to get the input data.

How can you make a php script only answer requests made from the same server?

How can I get my PHP script to only answer requests if the requesting script is on the same domain?
** Edit:
The PHP file is being accessed by an ajax request and is proxy, so I don't want others directly requesting it to come up, is this possible?
You could use $_SERVER['REMOTE_ADDR'] to compare the IP of the user requesting the page. Or you could simply make it a command line script that (obviously) requires you to run it from the command line.
edit:
You want to prevent people from using that script other than via AJAX? Impossible, as AJAX itself is executed by the client, as such the request starts there. And it will be always possible to call that script alone; you can make it harder, but you won't be able to prevent it.
There is no safe way to do that. Some developers will naively use the HTTP referrer header field, but anyone smart enough to abuse your ajax interface will have no problem forging the referrer.
You shouldn't be sending requests to your own server. You should include the file and execute the functions directly.

Categories