I am sending data from my iOS app to my server in json format. Here is the format.
{
"email" : "email",
"password" : "password"
}
How can I get these variables in PHP? I actually want to get the parameters like that
$json = $_POST['jsondata'];
$data = json_decode($json, TRUE);
$email = $data['email'];
$user_password = $data['password'];
If I can't get the data like this, please tell me how should I send the data so that the backend can accept my data?
Try below
Method 1:
$json = file_get_contents( 'php://input' ); // RECEIVE INPUT JSON STRING
$data = json_decode($json, TRUE);
$email = $data['email'];
$user_password = $data['password'];
Method 2:
fopen(DOCROOT."json.txt","w+");
file_put_contents("json.txt", $_POST);
$recoreds = file_get_contents('json.txt', true);
$json_a=json_decode($recoreds, true);
Related
How to modify this response
$url = "https://example.com";
$data = "{\"phone_number\":\"18868768"};
$len = strlen($data);
$headers = array();
$otp = request($url, $data, $headers);
the response is
{"status":0,"msg":"not Found"}
I want to modify to this :
{"status":1,"msg":"Found"}
I'll assume you're saving your response to a $response variable. You need to convert the JSON to an array to manipulate it, then convert it back to JSON. So to change it you would do:
$response = json_decode($response, true);
$response['status'] = 1;
$response['msg'] = 'Found';
$response = json_encode($response);
That being said, you really shouldn't be encoding your initial JSON in string form. Do this instead:
$data = json_encode([
'phone_number' => 18868768
]);
I don't get what do I do wrong when I try to get my values from post method from angular to php, it seems that everything is correct but then I am not able to see any values to postman/return the correctly.
this is my php method with all my attempts:
public function created()
{
$json = file_get_contents('php://input');
$json_string_in_array = array($json);
$json_array =json_decode($json_string_in_array[0]);
var_dump($json_string_in_array);
var_dump($json_array);
//return $json_array;
// $data = json_decode(file_get_contents('php://input'), true);
// $data = json_encode(file_get_contents("php://input"));
// $request = json_decode($data);
// var_dump($data);
// return $data;
// return $request;
// $json = file_get_contents('php://input');
//$obj = json_decode($json);
// get the raw POST data
// $rawData = file_get_contents("php://input");
// this returns null if not valid json
// return response()->json($rawData);
// DatabaseModel::DatabaseModel($json_array);
}
what I am trying to do is get that data and then send it to DatabaseModel in order to add it to database.
First you header, content-type, should be application/json. Are you manually setting the headers in angular? I am pretty sure angular will detect (or at least it is the default) the content type if its not set so in your case it would be application/json
<?php
$json = file_get_contents('php://input');
$request = json_decode($json);
$name = $request->name;
$email = $request->email;
# etc
?>
I made a cURL to Salesforce and got a responce like:
{"access_token":"XXXXXXXXX","instance_url":"https://na25.salesforce.com","id":"XXXXXXXXX","token_type":"Bearer","issued_at":"1458059021745","signature":"XXXXXXXX="}
I am trying to save the "access_token" like so but not working:
$result = curl_exec($ch2);
$session_id = $result['access_token'];
I have also tried:
$result = curl_exec($ch2);
$parsed = array();
parse_str($result, $parsed);
$session_id = $parsed['access_token'];
the API is returning a json string. you need to run it through json_decode first.
$result = curl_exec($ch2);
$resultArray = json_decode($result, true);
$session_id = $resultArray['access_token'];
you can try with below code
$result = curl_exec($ch2);
$result_aray = json_decode($result,true);
$session_id = $result_aray ['access_token'];
Let me know still anything.
i go from my front(login.php) to my back(loginback.php) i want to send an array of data from the back to the front. When i do this all it prints is "Array", so i tried using print_r to see what was in it and it still just says "Array".
login
<?php
if(isset($_POST['submit']))
{
include_once("dbconnect.php");
$name = $_POST['name'];
$pass = $_POST['pass'];
$account = $_POST['account'];
$post = 'name='.$name.'&pass='.$pass.'&account='.$account;
$url = "#####/Middle/loginBack.php";
$ch = curl_init();//initialize
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURL_POST, true); //true = 1
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//post is the date of name value pair
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//get data bak = true
$response = curl_exec($ch); //execute and get back data
curl_close($ch);// close all curl connections
print_r($response);
?>
loginback
<?php
include ('dbconnect.php');
$name = $_POST['name'];
$pass = $_POST['pass'];
$account = $_POST['account'];
$sql = " SELECT id, Username, Password FROM studentAccounts WHERE Username = '$name' ";
$query = mysqli_query($dbCon, $sql);
$row = mysqli_fetch_row($query);
$dbid = $row[0];
$dbName = $row[1];
$dbPass = $row[2];
$num = 100;
$myArray = array($num, $name, $dbid);
echo $myArray;
?>
It's not possible to print an array with echo. In your case since you want to grab the data from another server, you would need to send data in a controlled from. JSON would work fine here.
Change your echo to something like this
echo json_encode($myArray);
then you should see the JSON string representing your array. Now on the other side, you would use json_decode to convert it back to an array.
EDIT:
Just to make it clear, if you would replace
print_r($response);
with
print_r(json_decode($response));
i do think you will see your data.
you can use the below code to send array to the desired url using post method
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => AUTHENTICATIONURL,
CURLOPT_USERAGENT => 'user Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'0' => 'ram',
'1' => 'sham',
'2'=> 'geta',
'3'=>'so on'
)
));
Try this code snippet
and $post = 'name='.$name.'&pass='.$pass.'&account='.$account;
This is not an array it's just a string
curl -i -X POST -d 'json={"Amount":1000,"idPlayers":1}' http://www.myurl.com/updateamount.php
My php code:
<?php
$json = urldecode($_GET['jsonSendData']);
$json = str_replace("\\", "", $json);
$data = json_decode($json, true);
// if i hard code it here it works fine, but need from a post from curl
//$json = '{"Amount":1000,"idPlayers":1}';
$data = json_decode($json, true);
echo "json = ", $json;
$amount = mysql_real_escape_string($data['Amount']);
$id = mysql_real_escape_string($data['idPlayers']);
echo "Amount = ",$amount;
return;
Try to change to read post data and using the correct name:
$json = urldecode($_POST['json']);