Making XMLHttpRequest from php - 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 ?

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).

How to interpret CURL headers sent by user

I am creating an API using Laravel and Apache as the backend. My predicament is this, when I type a curl command in my terminal like,
curl -H "API KEY: NIKAPIKEY" --data "param1=value1&param2=value2" http://localhost:8888/awesome-calendar/public/config
How do I read the header API KEY in my php backend? Like I can read the POST parameters as $_REQUEST.
Answered by #nogad
The function apache_request_headers acts like the var_dump for all HTTP headers
http://php.net/manual/en/function.apache-request-headers.php

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

How to debug curl on IIS?

How to inspect CURL requests?
My PHP scripts are hosted on IIS and I want to find some debugging tool for CURL.
Could you suggest something in fiddler-style?
(Or maybe there is a way to use fiddler itself, I failed to do so because if I make my CURL to tunnel through proxy 127.0.0.1 it makes CONNECT requests instead of GET)
wireshark is not working for HTTPS but for HTTP only.
Can you change your curl script to use HTTP ?
Use curl -v for verbose mode.
From man curl
-v/--verbose
Makes the fetching more verbose/talkative. Mostly useful for debugging. A line starting
with '>' means "header data" sent by curl, '<' means "header data" received by curl that
is hidden in normal cases, and a line starting with '*' means additional info provided by
curl.
Note that if you only want HTTP headers in the output, -i/--include might be the option
you're looking for.
If you think this option still doesn't give you enough details, consider using --trace or
--trace-ascii instead.
This option overrides previous uses of --trace-ascii or --trace.

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

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.

Categories