Sending POST request from VueJS to PHP - php

<?php
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS,POST,PUT ");
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers,Origin, Accept, X-Requested-Wurh, Content-Type, Access-Control-Request-Method,Access-Control-Request-Headers ");
$curl = curl_init($_SERVER['PHP_SELF']);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER , true);
$data = curl_exec($curl);
if($data===false){
var_dump(curl_error($curl));
} else {
$data = json_decode($data, true);
var_dump($data);
}
curl_close($curl); ?>
updateCilent(){
let data = {"name":"john","lastname":"doe"};
axios
.post('http://127.0.0.1/devphp/projet/action.php',
data
)
.then(console.log(data))
.catch(err => console.log(err));
},
I've tried to send a post request from Vue JS to php ! I want to retrieve the value of 'codecli' from my "index.js" (index.js Image) file but I can't! The post request works correctly when I do console.log the data (data console.log Image) but I receive nothing in my php file (php code Image)! this is what it looks like on the browser (empty string)! Someone can help me ? Thanks in advance !

You can try to change php to:
<?php
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS,POST,PUT ");
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers,Origin, Accept, X-Requested-Wurh, Content-Type, Access-Control-Request-Method,Access-Control-Request-Headers ");
$data = json_decode(file_get_contents('php://input'), true);
echo "<pre>";
print_r($data);
echo "</pre>";

Related

How do i retrieve a data from php in godot?

I'm trying to get a data from php in my local server. from godot side it just works fine I can download an image. but when I'm trying to make a connection to a php I always get a HTTP error 405.
I've setup my godot and everything, and this is my code.
get_data.php
<?php
$item_db = file_get_contents("item_db.json");
if (isset($_SERVER['HTTP_ORIGIN'])){
header("Access-Control-Allow-Origin: *");
header("Access-Control-Max-Age: 60");
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS"){
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Authorization,
Content-Type, Accept, Origin, cache-control");
http_response_code(200);
die;
}
}
if ($_SERVER['REQUEST_METHOD'] !== "POST"){
http_response_code(405);
die;
}
switch ($_REQUEST['command']){
case "add":
echo "add item";
break;
case "edit":
echo "edit item";
break;
}
?>
gdscript
$HTTPRequest.request("http://localhost/get_data.php",
["Content-Type: application/x-www-form-urlencoded",
"Cache-Control: max-age=0"],
false,
HTTPClient.METHOD_POST,
"command=edit"
)

Post request cannot be sent

I want to send a post request for a url with curl.
This is my code to do this in the post.php file:
$url="http://localhost/result.php";
$api_key="PlC1QuHiXr6gSJ7fhVNjvGBcYUamn3xtsw";
$action="balance";
$send=[
'key'=>$api_key,
'action'=>$action
];
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$send);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$response=json_decode(curl_exec($ch),true);
$error_msg=curl_error($ch);
curl_close($ch);
$status=[
'curl_error'=>$error_msg,
'response'=>$response
];
var_dump($status);
And this is the file for which the post request should be sent:
header("Content-Type:application/json");
header('Access-Contorl-Allow-Methods: POST');
if($_SERVER['REQUEST_METHOD']==="POST"){
var_dump($_POST);
}
The problem is that nothing is posted and I can't find the error and the problem. What is the solution?

$_POST is empty when axios send POST request CORS

When I send POST request with json data in React app to PHP server, $_POST is empty.
And also php://input doesn't work.
I tried with following code.
PHP server side:
if($_SERVER['REQUEST_METHOD'] == "OPTIONS") {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization');
header('Access-Control-Max-Age: 1000');
header("Content-Length: 0");
header("Content-Type: text/plain");
} else if($_SERVER['REQUEST_METHOD'] == "POST") {
echo "Checking case1:\n";
echo "<br/>";
$data = json_decode(file_get_contents('php://input'), true);
var_dump($data);
echo "<br/>";
echo "Checking case2:\n";
echo "<br/>";
var_dump($_POST);
}
React side code:
axios.post(
'https://xx.xx.com/index2.php',
{
member: id
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*'
},
withCredentials: true
}
)
PHP server is running on CloudFlare.
I wonder if this is related with Cloudflare's cache process or not.
I hope any helps, Thank you in advance.
Use headers as below
It works for me on PHP
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
while sending from axios you need to change headers
headers: {
'Content-Type': 'multipart/form-data,multipart/form-data',
'Access-Control-Allow-Origin': '*'
}

Posting Data from Ionic App to PHP-File on Webserver via POST wont work

right now im trying to send datat from my ionic app to a php-file which should use that data for a MySQL-statement. unfortunately the file cant access the data and since im kinda new to HTTP-Requests i dont know why. As far as i can see the request is a valid one.
Below u can see my php code.
i Hope you can help
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE,
OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-
Token');
$postdata = file_get_contents("php://input");
$argument = json_decode($_POST["argument"]);
$mysqli = new mysqli(//DB connection is set here);
$query = $argument;
$dbresult = $mysqli->query($query) or trigger_error($mysqli->error . "
[$query]");
while ($row = $dbresult->fetch_array()) {
$json_row = $row['num'];
}
if ($dbresult) {
$result = $json_row;
}
else {
$preresult = "{'success':false}";
$result = json_encode($preresult);
}
echo json_encode($json_row);

Post method works in raw but not in x-www-form-urlencoded POSTMAN

This might be stupid but I hope you guys could enlighten me why this works
Image: POST using RAW
But using x-www-form-urlencoded makes all value null
Image: POST using x-www-form urlencoded
here's the php side
<?php
// required headers
header("Access-Control-Allow-Origin: http://localhost/mediapp/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// files needed to connect to database
include_once 'config/database.php';
include_once 'Objects/user.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
// instantiate user object
$user = new User($db);
// get posted data
$data = json_decode(file_get_contents("php://input"));
I also tried changing the content type but results are still the same. Am I not using POSTMAN properly, do I need to change something in php side? I need it to also work with x-www-form-url-encoded
x-www-form-urlencoded will send the body like
firstname=mikey&lastname=dalisay
that why json_decode() return null, use parse_str to convert it to PHP object
parse_str(file_get_contents("php://input"), $data);
var_dump($data);
echo $data['firstname'];
if you want to accept both raw and x-www-form-urlencoded you can write like
$reqBody = file_get_contents("php://input");
$data = json_decode($reqBody);
if(!$data){
parse_str($reqBody, $data);
}

Categories