Issue Sending Post Data To PhP - php

The Library Im Using: https://github.com/Code-Building/requests
PHP
if (register($_GET['username'], $_GET['password'], $_GET['hwid'])) { ... }
```
C++
http::request* result = http::post("https://xxx/api/handle.php?t=", data);
printf("%s", result->text.c_str());
I would like to add running this on a local server works just fine, having issues with livenet since thats what our teacher wants.
Also the issue is sending the post data. Manually entering post data / sending with fiddler work just fine. Any Ideas?
(Ive also used $_POST didnt work aswell)

Related

Laravel Axios Doesn't Send Correctly Data to Backend Then Returns 422 Status Code

My code, which I tested yesterday and worked correctly, is not working now. I've had some results by trying to read the error messages.
First of all, I want to say that this is a login process. Using Axios, I receive the data in the form and send it to the back-end, and if everything is correct, I log in and redirect it to the dashboard. A classic login process.
Today, when I wanted to log in to develop new things, I saw the error code 422 returned, and when I printed the incoming data on the server-side, I saw something like this:
// $request->all(); result
Array
(
[------WebKitFormBoundary0OBe87BBjSLuzX6z
Content-Disposition:_form-data;_name] => "email"
example#example.com
------WebKitFormBoundary0OBe87BBjSLuzX6z
Content-Disposition: form-data; name="password"
example.123!
------WebKitFormBoundary0OBe87BBjSLuzX6z--
)
// Laravel Error
{"message":"The given data was invalid.","errors":{"email":["The email field is required."],"password":["The password field is required."]}}
I haven't made any changes or updated anything in this part of the system for about three months. So I don't understand why it's not working. However, the form element comes incorrectly on the javascript side, and the Axios request is sent successfully. Here is where I prepared the form:
let data = new FormData(form);
axios.post(url, data).then(function (response){
...
}
When I examined the Axios versions (I'm using it via CDN), I saw that it was due to an error in the v0.27.1 version. However, when I looked at Unpkg from the official CDN links, I saw that it was upgraded to the latest version, and when I included the Unpkg cd for testing, the problem was solved.
This version fixes the problem: https://github.com/axios/axios/releases/tag/v0.27.2.

Issues with UnityWebRequest.Post not posting data to PHP

My project requires contacting a PHP script hosted online. First the project should post a string to the PHP to be processed and returned from the PHP. So in my C#, I call UnityWebRequest.Post followed by UnityWebRequest.Get. Only the Post has issues. The Get request works perfectly as intended (the PHP returns a default set of data if the Post request doesn't do anything).
List<IMultipartFormSection> dataForm = new List<IMultipartFormSection>();
dataForm.Add(new MultipartFormDataSection("difficulty=Easy"));
UnityWebRequest www = UnityWebRequest.Post(webpage, dataForm);
yield return www.SendWebRequest();
This was the latest method I attempted using. Previously I used WWWForms and also tried to Post the string directly into the method. None of these worked. The Get request in Unity works fine, so I figured it might be the PHP that might be the problem.
$difficulty = $_POST["difficulty"];
if (!isset($difficulty)) {
$difficulty = "Hard";
}
But I wasn't convinced the PHP was broken since it works perfectly if $difficulty is set using the GET method through the URL (currently the project is using WebClient from System.Net and works perfectly which further convinces me that the PHP isn't the issue). So what's exactly the issue with UnityWebRequest.Post here?
You need to do a comma not equal operator.
dataForm.Add(new MultipartFormDataSection("difficulty", "Easy"));

Python to PHP on server send image

I have a raspberry pi running a lamp stack, an arduino and a camera hooked up. The end goal is that when my arduino takes a photo, it then writes an image to a php address which is then emailed.
Right now, I'm trying to get the image to get placed in the right place.
Here's my php snippet:
<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/images/mypic.jpg");
?>
My python code is doing:
import requests
r = requests.get('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')
r2 = requests.post('http://192.168.1.100/accept_image.php', data = r.content)
I realize the image is going to get overwritten. That's not a problem. I can always add a timestamp later etc etc.
However, this gives me an error code. I'm a beginner at php and use python mainly for scientific computing so not sure if I'm passing the picture correctly. I know that the ip is correct as I can connect to it and it's all in network.
I have looked at this Python script send image to PHP but am still getting stuck.
EDIT:
Upon further debugging:
print_r($_POST);
returns an empty array. Not sure why?
To have a file accessible to PHP in $_FILES, you must use HTML-form style encoding of files (multipart/form-data). This is different from a standard POST request including the content in the request body. This method is explained in http://php.net/manual/en/features.file-upload.post-method.php - the key here is:
PHP is capable of receiving file uploads from any RFC-1867 compliant browser.
What's you're trying to do is not sending it the RFC-1867 way, but a plain-old POST request.
So you have two options:
Send the data from Python using multipart/form-data encoding. It shouldn't be too hard but requires some work on the Python side.
Just grab the data on the PHP side not from $_FILES but by directly reading it from the POST body, like so:
.
$data = file_get_contents('php://input');
file_put_contents("/var/www/images/mypic.jpg", $data);
It's more of a memory hog on the PHP side, and means you need to do some validation that you actually got the data, but is quite simpler.
To clarify, in PHP $_POST is only populated when the Content-type request header is multipart/form-data or application/x-www-form-urlencoded (and of course the data is encoded in the proper way).
If you get a POST request with anything else in the body, you can read it directly by reading from the php://input stream, and you're responsible for handling / decoding / validating it.

POST Values are not recognized

I'm using the Zend Framework and jQuery.
I'm calling a JavaScript function, which sends POST-Data to a PHP scripts and waits for an answer.
Everything worked fine this morning. I'm using Debian.
The values are send via the header, Firebug gives me the following Output:
Parameterapplication/x-www-form-urlencoded
endDatum
funktionsId 1
mitgliederId 10368
startDatum 31.03.2013
Quelle
mitgliederId=10368&funktionsId=1&startDatum=31.03.2013&endDatum=
But neither the $_POST Variable or the Zend Framework Method $this->getRequest()->getParams(); gives me anything.
Any idea what's wrong?
Maybe something is wrong with my php.ini Settings?
What settings can I change to modify the header-output?
Solved it: the problem was the subdomain i set up for the page. The AJAX-Reuquest weren't called correctly
Try this for getting post values...
$this->getRequest()->getPost();
or for individual form elements
$this->getRequest()->getParams('elementName');

Strange behaviour when trying to send data with ajax (jQuery)

I'm coding an website which must have same functionality as SO. My server use Litespeed, which increase the speed of runing php files.
When I'm trying to send php code in regular form, everything seems to be ok, data being sent to the action file without interpreting the code inside the variable. But when I'm trying to send the same data in comments textareas, with jQuery $.post method, I'm getting Forbidden 403 error. Data is blocked on the road by Litespeed server. I don't know why is happening this, why is $_POST data auto-evaluated when using ajax?
This situation doesn't appear everytime, but just in some cases, for example:
this is a new message which presents this php code: <?php mysql_query("SELECT * FROM my_table"); ?>
Note that if I remove <?php and ?> from my message, it is ok. I've already tried to use encodeURI() method but this don't change anything.
Is there any setting which must be applied in .htaccess for not auto-evaluate $_POST data?
or I should replace the open and close tags, before sending the comment?
Later edit
This is how I managed to resolve this right now, but I'm still waiting for a suggestion with .htaccess:
$but.prev('textarea').val().replace(/(<\?php)/gi,'-?php ').replace(/(<\?)/gi,'-? ').replace(/\?>/gi,' ?-');
and on the server side
str_replace(array('-?php','?-','-?'), array('`<?php','?>`','`<?'), $_POST['comment']);

Categories