Why doesn't PHP populate $_POST with the passed parameters? - php

I write a simple web server in JavaScript with NodeJS. I have the problem, that PHP doesn't populate $_POST with the passed parameters. What I already achieved is, that $_GET is populated with the passed parameters.
process.env["GATEWAY_INTERFACE"] = "CGI/1.1";
process.env["SCRIPT_FILENAME"] = path.resolve(resource);
process.env["REQUEST_METHOD"] = request[0];
process.env["REDIRECT_STATUS"] = 200;
process.env["QUERY_STRING"] = queryString;
process.env["CONTENT_LENGTH"] = queryString.length;
process.env["CONTENT_TYPE"] = "application/x-www-form-urlencoded";
content = execSync(phpPath +"php-cgi", process.env);
But print_r($_POST) gives me Array ( ). If I use GET it works and the parameters are in $_GET. My assumption is, that the problem is somewhere in the environment variables. queryString.length is the size of the POST parameters given by the browser

I got it working. I had to pipe the query string as stdin input for php-cgi:
content = execSync(phpPath +"php-cgi", { env: process.env, input: queryString });
This only works with POST that's why I check for POST and GET now and set the method accordingly

Related

Accessing POST parameters from web form using Request and not $_POST Symfony 3

I have a web form that submits some hidden input fields with post parameters and I want to use Request object from Symfony.
This is how I'm doing it now using an API based request also with Postman app I can access the son values like this.
myAction(Request $request){
$content = $request->getContent();
$params = json_decode($content, true);
$value = $params['value'];
}
But when i use a web form it doesn't get the values this way. I was trying to figure out how to get the values and i ended up using the post variable which works fine.
$value = $_POST['value'];
I don't want to use the global variable but rather grab the value from the request. I don't have a super good reason why, other than I prefer it the Request way. Any help would be appreciated.
Is there something special I'd have to do with the HTML form?
Use $value = $request->request->get('value'); to get a single POST value.
Use $values = $request->request->all() to get all POST values.
From symfony docs

Php - Post variable values' part at specific index is missing

I'm making a POST request to my application with a long stringified JSON like below:
/////POST PARAMETER VALUE
{"objects":[{"type":"path","originX":"center","originY":"center","left":118.63,"top":252.5,"width":41,"height":139,"fill":null,"overlayFill":null,"stroke":{"source":"function anonymous() {var patternCanvas = fabric.document.createElement('canvas');patternCanvas.width = patternCanvas.height = 10;var ctx = patternCanvas.getContext('2d');ctx.stroke`Style = quotesquare005E7Aquote;ctx.lineWidth` = 5;ctx.beginPath();ctx.moveTo(5, 0);ctx.lineTo(5, 10);ctx.closePath();ctx.stroke(); return patternCanvas;}","repeat":"repeat","offsetX":0,"offsetY":0},"strokeWidth":15,"strokeDashArray":null,"strokeLineCap":"round","strokeLineJoin":"round","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"selectable":true,"hasControls":true,"hasBorders":true,"hasRotatingPoint":true,"transparentCorners":true,"perPixelTargetFind":false,"shadow":null,"visible":true,"clipTo":null,"path":[["M",40.5,0],["Q",40.5,0,41,0],["Q",41.5,0,41.25,2],["Q",41,4,38,16.5],["Q",35,29,29.5,42],["Q",24,55,21,65],["Q",18,75,15.5,83.5],["Q",13,92,9.5,101.5],["Q",6,111,4.5,118],["Q",3,125,2,128],["Q",1,131,0.5,134],["Q",0,137,0,138],["L",0,139]],"pathOffset":{"x":0,"y":0}}],"background":""}
////// END
Every time i do this request, value comes missing. In the server the part that i highlighted is lost. I realised that it's a specific index range at the string. When i give another data in this json stringified format, it looses again the part in that index range. And i'm sure that i'm sending the value complete because i checked it at the request headers.
The problem is not about my code because i tested it with POSTMAN app and the result is same.
Anyone can help about this misterious situation. Is there anything in this value which causes some escaping. And i'm using Codeigniter.
Thanks

Parsing the incoming request in PHP

If the incoming PHP possible requests can be the following
1)http://wwww.test.com/api.php?device_id=xxxx&user_name=xxxx&pass_word=xxxx&num_request=xxxx&num_site=xxxx;
how do I extract the user_name field from the above in my api.php code?
Those values are passed via a standard GET request accessible like this:
$deviceid = $_GET['device_id'];
Be aware that this value is unsanitized and open to modification from the user which could ultimately end up with SQL injection depending on your usage of the value.
You can use the $_GET superglobal to acess querystring values
$user_name = $_GET['user_name'];

Receive http $_POST and feed into SimpleXML

I'm currently trying to setup a page which receives XML via an HTTP POST. I have successfully used SimpleXML to retrieve the XML from a file and then perform my logic, but I am unsure how to set it up to receive a POST submission.
Is there a default way to retrieve all information from $_POST as a string?
//'get'ting the xml from a file
$job = simplexml_load_file(/path/to/file);
//my assumption on how to accept the XML post - throws a not string error
$job = simplexml_load_string($_POST);
As the is being received from a third party, is there extra information that I am not being supplied? All my previous handlings have been with name=value pairs, i.e. $value = $_POST['name']; To rephrase, do all HTTP POSTs have a name handle to them?
Sorry for the multi-faceted question, I'm a bit lost, so am trying to cover all angles.
Any help is greatly appreciated!
You're most likely looking for the raw POST data.
$postdata = file_get_contents("php://input");
This code will combine all posted variables into a single string variable:
$foo = "";
foreach( $_POST as $val )
{
$foo .= $val;
}
well, if you are receiving xml with using post, why not are you using xmlrpc?

Passing info via URL and parsing it with PHP

I need to pass information via a URL to a PHP function that parses the received data.
So basically when they click on the specific link, it will go to the specific page, but also send through the arguments and values for the waiting PHP function to parse them and depending on what the values are do specific functions.
eg: http://www.mydomain.com/arg=val&arg=val&arg=val (not sure about how I built that URL either)
Your can access the parameters via the array $_GET.
Note: in your example, you basically send just one paramter. PHP cannot handle multiple parameters with the same name.
You can build URLs that contain data from variables using string concatenation or string parsing.
If you browse the web, you will recognize, that the query string (the parameters) always follow a ? in the URL. Read about URLs and the URI syntax.
Probably also interesting for you to read: Variables From External Sources which describes how to deal with data sent via POST or GET or cookies.
Generate URL:
<?php
$data = array(
'first_name' => 'John',
'last_name' => 'Doe',
'motto' => 'Out & About',
'age' => 33
);
$url = 'http://example.com/?' . http_build_query($data);
?>
Pick data:
<?php
$data = array();
if( isset($_GET['first_name']) ){
$data['first_name'] = $_GET['first_name'];
}
// Etc.
?>
You can access url parameters that follow the ? in at the end of the URL using the $_GET superglobal, e.g.
// url: http://mydomain.com/mypage.php?arg=Hello
echo $_GET['arg'];
Parameters passed in the query string are available in the $_GET superglobal array.
You can build the URL like the following (note the ? )
http://www.domain.com/page.php?arg1=val&arg2=val
Then in your PHP using the following code by using REQUEST you can get the data from either GET or POST parameters.
$arg1 = $_REQUEST['arg1'];

Categories