I am using the code below to base 64 decode a token
list($header, $payload, $signature) = explode (".", $token);
$jsondata = base64_decode($payload);
$data = (array) $jsondata;
$oSession->aSSO["email"] = $data["emails"];
$oSession->aSSO["customerId"] = $data["CustomerId"];
If I do var_dump($data);
I get
array(1) { [0]=> string(411)
"{"nbf":1572801391,
"exp":1572801691,
"iss":"ISS",
"aud":"AUD","nonce":"NONCE",
"iat":IAT,
"sid":"SOD",
"sub":"SUB",
"auth_time":1572800662,
"idp":"IDP",
"CustomerId":"CUSTOMERID",
"emails":"EMAIL",
"amr":["pwd"]}"
}
How do I get access to emails and CustomerId?
These are both coming back blank even though we can see from the var_dump that they are present
I have tried data[0]->CustomerId with no joy either
What you should be able to do is JSON decode the string without casting it to an array.
list($header, $payload, $signature) = explode (".", $token);
$jsondata = base64_decode($payload);
$data = json_decode($jsondata, true);
$oSession->aSSO["email"] = $data["emails"];
$oSession->aSSO["customerId"] = $data["CustomerId"];
Related
I send the following data to my php file via ajax:
JSON.stringify({test1: '1', test2: '2'})
I would like to write this data to my JSON FILE content.json containing an empty array
[]
I only want to add the content though if it is not there, yet. This is my PHP code:
$jsonStringObject = file_get_contents("php://input");
$phpObject = json_decode($jsonStringObject);
$newJsonStringObject = json_encode($phpObject);
header('Content-Type: application/json');
$jsonString = file_get_contents('content.json');
$data = (array) json_decode($jsonString, true);
if (in_array($phpObject, $data) === false){
$data[] = $phpObject;
}
$newJsonString = json_encode($data);
file_put_contents('content.json', $newJsonString);
It almost works. Something is wrong with the way the data is added to the array because when I call the function for the first time, it updates content.json to
[null,{"test1":"1","test2":"2"}]
On calling the function again, it adds the object again despite the if-statement:
[null,{"test1":"1","test2":"2"},{"test1":"1","test2":"2"},{"test1":"1","test2":"2"}]
Can anyone help me to spot the mistake?
You're comparing apples and oranges (or rather objects to arrays).
Make sure you decode all json as arrays:
$jsonStringObject = file_get_contents("php://input");
// Added true as second argument to get it bas as an array
$phpObject = json_decode($jsonStringObject, true);
$jsonString = file_get_contents('content.json');
// Removed the (array) since the second argument literally means "return as array"
$data = json_decode($jsonString, true);
if (in_array($phpObject, $data) === false){
$data[] = $phpObject;
}
$newJsonString = json_encode($data);
file_put_contents('content.json', $newJsonString);
Demo: https://3v4l.org/rnCn5
i am tring to validate my openmoney webhook url but it is not working,i am getting all in $payload and then remove hash from $payload them convert it in string . and then hash_hmac but don't know what is problem ..
$hashed_expected = $request->hash;
$payload = $request->all(); // get all body
unset($payload['hash']);
$str = json_encode($payload); // Convert in string
$string = preg_replace('/\s+/', '', $str); // remove white space
$service = DepositServices::where('type','open_money')->first();
$apiSecret = $service->details->api_secret; // open money API Secret
$hashed_value = hash_hmac('sha256',$string,$apiSecret);
$data['hashed_value']=$hashed_value;
$data['hashed_expected']=$hashed_expected;
$data['string'] = $string;
if (hash_equals($hashed_expected, $hashed_value) ) {
$data['status'] = "Match";
dd( $data);
}else{
$data['status'] = "Not Match";
dd( $data);
}
When creating and comparing your hashed values, the payload needs to be identical as it was when the sender created and sent it. My guess is that the encoding and preg_replacing is mutating the payload.
Something like this should get you the original, unaltered payload body.
json_encode($request->all(), JSON_UNESCAPED_SLASHES);
I am using PHP with cURL to make requests to an API.
The API responds with an encrypted string which I then have to use json_decode on and run it through a pre-defined decrypt method that returns a string.
So I have something like this:
echo $response;
$decodedResponse = json_decode($response, true);
// New instance of Decrypt
$decrypt = new Decrypt();
$decryptedResponse = $decrypt->decrypt($decodedResponse);
echo $decryptedResponse;
Using var_dump($decryptedResponse) yields string(960) but the string looks like a JSON array.
{"Title":"Mr","Forenames":"Steve"}
So what is the best way to rip apart this string so that I can use the variables through an associative array?
I had already tried:
foreach(decryptedResponse as $data)
{
echo $data['Title'];
}
But this outputted nothing on the screen.
Am I misinterpreting the use of json_decode?
As many have stated it seems you have to decode twice, I'll look into this and share my findings.
You need to json_decode again on the decrypt result
$decodedResponse = json_decode($response, true);
// New instance of Decrypt
$decrypt = new Decrypt();
$decryptedResponse = $decrypt->decrypt($decodedResponse);
$decryptedArry = json_decode($decryptedResponse, true);
var_dump($decryptedArry);
echo $decryptedArry['Title'];
As you told Using `var_dump($decryptedResponse)` yields string(960) but the string looks like a JSON means your decrypt duration convert it again json. You can try bellow code it may resolve your issue
$decodedResponse = json_decode($response, true);
// New instance of Decrypt
$decrypt = new Decrypt();
$decryptedResponse = $decrypt->decrypt($decodedResponse);
$decryptedResponse = json_decode($response, true);
foreach(decryptedResponse as $data)
{
echo $data['Title'];
}
The code below shows that the json_decode works like you want it to but it seems like your Decryption class does something weird.
$response = '{"Title":"Mr","Forenames":"Steve"}';
$decodedResponse = json_decode($response, true);
var_dump($decodedResponse);
echo $decodedResponse["Title"];
I have a .txt file called 'test.txt' that is a JSON array like this:
[{"email":"chrono#gmail.com","createdate":"2016-03-23","source":"email"}]
I'm trying to use PHP to decode this JSON array so I can send my information over to my e-mail database for capture. I've created a PHP file with this code:
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
echo $json;
?>
For some reason, it's not echoing the decoded JSON when I visit my php page. Is there a reason for this and can anyone shed some light? Thanks!
You use echo to print scalar variables like
$x = 'Fred';
echo $x;
To print an array or object you use print_r() or var_dump()
$array = [1,2,3,4];
print_r($array);
As json_decode() takes a JSON string and converts it to a PHP array or object use print_r() for example.
Also if the json_decode() fails for any reason there is a function provided to print the error message.
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo json_last_error_msg();
exit;
}
You'll need to split that json string into two separate json strings (judging by the pastebin you've provided). Look for "][", break there, and try with any of the parts you end up with:
$tmp = explode('][', $json_string);
if (!count($tmp)) {
$json = json_decode($json_string);
var_dump($json);
} else {
foreach ($tmp as $json_part) {
$json = json_decode('['.rtrim(ltrim($json_string, '['), ']').']');
var_dump($json);
}
}
I need to decode this json value using PHP. I need the value 'url'.
Here is what I have but does not work.
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded[???]; // NEED THIS VAR.
Since $decoded is a PHP array you can access it like any other array:
$url = $decoded['data']['url'];
$decoded=(object)json_decode( $json['data'], true );
echo $decoded->url
This worked:
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded['data']['url'];