Determining URL of script that is calling from a remote CURL request - php

I have a homebase script, that I have many other scripts ping for information using the CURL method. I need to determine the domain name of the callers. Can I do this with tricks just on my homebase script?
. using php .
Hudson

You could send a custom HTTP header with your CURL request that contains the script name, something like
X-SCRIPT-NAME myscript.php
I don't think CURL automatically adds something about the calling script, so you would have to edit the scripts for this.

Related

Is it possible to obtain POST data in NodeJS without creating a server?

I want to replace the PHP script that currently handles receiving and processing data from my webpage (LAN only) by a NodeJS file. Currently, the JSON data is being sent in JS with an XMLHttpRequest:
var xhttp = new XMLHttpRequest();
var url = "/server.php";
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.onreadystatechange = function () {
...
};
xhttp.send(content);
Obviously, server.php is the file I'm looking to replace. In this file, I receive the data like this:
$stringHttp = file_get_contents("php://input");
I have searched far and wide on how to do something like this in NodeJS, but everything I find uses this basic layout:
http.createServer((request, response) => {
...
}).listen(8091);
Now, since my webpage is hosted by Apache, it's probably not possible to create this server on the same port. At least, that's what I'm getting from the error message I get when I try to run the NodeJS file:
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::8091
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1355:14)
at listenInCluster (net.js:1396:12)
at Server.listen (net.js:1480:7)
at Object.<anonymous> (/var/www/apache/testNode.js:15:4)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
So basically, I'm looking for a NodeJS replacement of file_get_contents("php://input").
Hence my question: Can you obtain POST data in NodeJS without creating a server?
No, it isn't.
In your PHP version, you have a server. It is Apache and it makes use of (for example) mod_php to execute the PHP.
If you are executing your program with Node.js, then you need some way to get the HTTP request to the program. That involves running a server.
it's probably not possible to create this server on the same port
No. You'd need to run it on a different port. (And then either post to it directly or configure Apache to act as a proxy in front of it).
Well, if you really just want to run a node.js script one and done to get a result, then you could keep a shell of your PHP script and have it run node and your script. You could either pass the node script the required data on the command line or you could send it in stdio and the node script would grab it from whichever.
Your node script would run and create the desired result and write it to stdout. The PHP script would then grab the stdout data and forward it as the http response.
Nobody is going to describe this a super optimal way to do things. HTTP response is sent to Apache, then fires up the PHP interpeter to run your PHP script which fires up node.js to run your node.js script. But, if it's a one-off thing just for this one use and there's some compelling reason that you use PHP elsewhere and need node.js for this one thing, then you could make it work.
It may even be possible to create a custom path for this one script that Apache could be configured to detect and then run your node script directly (like it does for PHP) without the PHP middleman. I don't know Apache well enough to advise exactly how to do that, but there are some references on doing it. Again, not optimal, but it could be made to work.
The best performance solution would be to actually create a node.js server on another port and have either Apache or some other proxy detect certain requests (usually based on the path) that you want redirected to your node.js server rather than sent through to PHP or post directly to the other port from the client (you'd have to enable CORS in your node.js server to allow that to work).

Making XMLHttpRequest from php

I want to do some m2m-communication, where my server is to emulate a human operating a webpage.
So I'm trying to SEND an XMLHttpRequest from php TO another server.
Whatever I've searched for gives how for php to ACCEPT a XMLHttpRequest
I have debugged the browser, and Chrome webdeveloper tools have given me a cURL cmd which works.
The curl cmd ends in
--data-binary '[{"productNumber":"12345678","quantity":1}]'
I'm using snoopy to send the requests, and have emulated every cookie and header, but the server still responds with 400 Invalid Request.
I think the problem lies in that snoopy usually is used like this:
$submit_vars['email'] = "johndoe#example.com";
$submit_vars['password'] = 'secret';
$snoopy->submit($submit_url, $submit_vars);
i.e. Snoopy expects an array of form variables, not a string.
Is there a way to make snoopy send the equivalent of curl --data-binary ?

PHP Header in CRON Job not working

I am using this code header('Location: http://example.com/test.php?number='.$requestsDone.'');
But looks like it is not working, what is wrong here?
Let me know, if you need more information.
Producing a header in a command line script doesn't make any sense. The header is part of the HTTP protocol, there is no HTTP involved when the script is executed using the CLI version of PHP.
Accordingly, the header() function is not implemented in the CLI version of PHP. It exists, but it doesn't produce any output.
Also, the superglobals that contain information extracted from the HTTP request ($_GET[], $_POST[], $_REQUEST[], $_FILES[], $_COOKIE[] etc) exist but they are empty.
In order to pass arguments to a script using the command line, use the $argc and $argv[] variables.

How to pass POST parameters to Chromium via the command line?

I use chromium --ingognito www.mysite.com/page.php?msg=mymessage to open my website and pass it a msg.
I wish to know how to pass the same msg param via POST instead to use GET, from command line.
Do you do anything with the site in Chromium after opening it? Otherwise you could use a more capable command line http client like curl(1) which would make this very easy.
See this example:
curl --data "param1=value1&param2=value2" http://example.com/resource.cgi
With console ? I don't know, but you can try to use this extension : Advanced REST Client.
The web developers helper program to create and test custom HTTP requests.
Here is the link : https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

Howto run a php file from a other php file?

Is it possible to launch a php file from a other php file?
I know that i can include a file but i don't mean this.
Small example:
i have a script which displays something from the database
and a other script which get the latest data from a other site and update the database.
When i include the update file, the first script will request the data from the server but i want to make it parallel so that only the update script request from server
Yes, and you can use include provided fopen wrappers are turned on.
include("http://otherserv.com/path/to/script.php");
If you don't care about the reponse from the other server, you can do
get_headers("http://otherserv.com/path/to/script.php");
This will complete much faster if the remote script takes time to process.
Yes, you need CURL (or any other HTTP library) to "run" a php file from another as you described in your scenario.
So, in your case:
CLIENT will run a REQUEST to SERVER1 (a.php)
SERVER1's a.php, will use CLIENT REQUEST to perform a remote PHP file in SERVER2 (b.php)
SERVER2's b.php will process SERVER1's REQUEST and place an appropriate response
SERVER1's a.php will receive SERVER2's b.php RESPONSE, parse it, and send appropriate response to CLIENT.
Hope it clarifies something to you.
you should look at exec http://au.php.net/manual/en/book.exec.php

Categories