Equality comparison PHP - php

I'm doing this comparison below: $deviceKey == 'mobile' or $deviceKey == 'desktop'
public function syncPublisherIds (string $publisherId, Request $request):array
{
file_put_contents('/var/log/php/burner.log', json_encode($request->getContent()) . PHP_EOL, FILE_APPEND);
$idSyncConfig = json_decode($request->getContent());
$idSyncResponseObject = new \stdClass();
foreach($idSyncConfig as $deviceKey => $deviceValue) {
// file_put_contents('/var/log/php/burner.log', json_encode(gettype($deviceKey)) . ' '. json_encode($deviceValue) . PHP_EOL, FILE_APPEND);
// $deviceKey = json_encode($deviceKey);
// add device type to object
if ($deviceKey == 'mobile' or $deviceKey == 'desktop') {
$idSyncResponseObject[$deviceKey] = new \stdClass();
} else {
throw ((new BadRequestException("Platforms must be of type 'mobile' or 'desktop', you sent type of: $deviceKey"))->errorize());
}
}
}
For some reason it hits the else statement when they are seemingly the same:
"Platforms must be of type 'mobile' or 'desktop', you sent type of: \"mobile\""
This is a Laravel endpoint.

<?php
function testForLuke(string $requestContent): void
{
$idSyncConfig = json_decode(stripslashes($requestContent));
//print_r([$requestContent, $idSyncConfig]);
foreach ($idSyncConfig as $deviceKey => $deviceValue) {
//print_r([$deviceKey, $deviceValue]);
if ($deviceKey === 'mobile' || $deviceKey === 'desktop') {
echo "great success with deviceKey of '${deviceKey}'!\n";
} else {
echo "error condition throw exception\n";
}
}
}
testForLuke('{ "mobile": { "id": 1 } }');
testForLuke('{ "desktop": { "id": 1 } }');
testForLuke('{ "derptop": { "id": 1 } }');
testForLuke("{ \"mobile\": { \"id\": 1 } }");
testForLuke("{ \"desktop\": { \"id\": 1 } }");
For me yields:
great success with deviceKey of 'mobile'!
great success with deviceKey of 'desktop'!
error condition throw exception
great success with deviceKey of 'mobile'!
great success with deviceKey of 'desktop'!
The code seems to be working already, but is highly dependent on the structure of the provided JSON, so depending on the structure of the request you may still have issues, let us know.
Edit: After re-looking, it seems like you've got backslashes in your JSON request. Are you encoding it yourself? If so you should use json_encode($response, JSON_UNESCAPED_SLASHES);
If you can't control how it is encoded for whatever you reason, you could consider using:
$idSyncConfig = json_decode(stripslashes($requestContent));

Related

GET JSON provides two values, I want to add these two togther using php, and then return to callback

$_GET['numberofwelds']; & $_GET['numberofconwelds']; are sent to this script using GET JSON. I want to add these together, and then use json_encode to send a total back to the callback (in another php script) . If both $_GET 's are empty, then I want nothing to happen. How should I change this?
$numberofwelds = $_GET['numberofwelds'];
$numberofconwelds = $_GET['numberofconwelds'];
if (isset($_GET['numberofwelds']) && $_GET['numberofwelds'] != '' {
$numberofwelds + $numberofconwelds = $sum_total;
echo json_encode($sumtotal);
} else {
exit()
}
Firstly, you are trying to access your $_GET variables without checking they exist first.
Secondly, you should be throwing Exceptions instead of just calling exit() or die(). You can then log them with $e->getMessage() or write them to the local filesystem.
Finally, you need to validate your data. Make sure it is what you expect it to be.
if (isset($_GET['numberofwelds']) && isset($_GET['numberofconwelds']))
{
// Now we know both values definitely exist, VALIDATE them
$numwelds = $_GET['numberofwelds'];
$numconwelds = $_GET['numberofconwelds'];
if (is_int($numwelds) && is_int($numconwelds))
{
// Calculate your total
$total = $numwelds + $numconwelds;
echo json_encode($total);
}
else
{
// We get here because your GET variables do exist but they aren't
// numbers as you expect (you or someone else has sent rubbish data)
// You want to do nothing, although I would return an error in your json
// to be displayed to the user or logged by the consumer of the service
}
}
else
{
// We get here because your GET variables simply don't exist. They haven't been
// passed in as you are expecting them to be
// You want to do nothing, although I would return an error in your json
// to be displayed to the user or logged by the consumer of the service
}
Always code defensively.
I'm going to show you what I would do in this situation.
if (isset($_GET['numberofwelds']) && isset($_GET['numberofconwelds']))
{
$numwelds = $_GET['numberofwelds'];
$numconwelds = $_GET['numberofconwelds'];
if (is_int($numwelds) && is_int($numconwelds))
{
$total = $numwelds + $numconwelds;
$response = array("status" => "success", "message" => $total);
echo $response;
}
else
{
$response = array("status" => "failure", "message" => "GET params were not numbers");
echo $response;
}
}
else
{
$response = array("status" => "failure", "message" => "GET params do not exist");
echo $response;
}
Then, in your consuming service (most likely a JavaScript / jQuery AJAX call), you can do the following:
.done(function(data) {
var json = $.parseJSON(data);
if (data.status === "success") {
// Yay, we got a success back
console.log("The total is: " + data.message);
} else if (data.status === "failure") {
// Uh oh, something's gone wrong server-side
console.log(data.message);
}
});
change this
if ($_GET['numberofwelds'] != '' && $_GET['numberofconwelds'] != '') {
$numberofwelds + $numberofconwelds = $sum_total;
echo json_encode($sumtotal);
} else {
exit()
}
to this
if ($numberofwelds && $numberofconwelds ) {
$sum_total = array(
'sumTotal' => $numberofwelds + $numberofconwelds,
);
echo json_encode($sum_total);
}else {
exit();
}
Please always check existence of the array keys with isset() construction or array_key_exists() function.
if (isset($_GET['numberofwelds']) && $_GET['numberofwelds'] != '' && isset($_GET['numberofconwelds']) && $_GET['numberofconwelds'] != '') {
echo json_encode(array("total" => $_GET['numberofwelds'] + $_GET['numberofconwelds']));
} else {
exit();
}
UPDATE
With is_numeric() function this code should be more robust:
if (isset($_GET['numberofwelds']) && is_numeric($_GET['numberofwelds']) && isset($_GET['numberofconwelds']) && is_numeric($_GET['numberofconwelds'])) {
echo json_encode(array("total" => $_GET['numberofwelds'] + $_GET['numberofconwelds']));
} else {
exit();
}
PHP reference: array_key_exists()
PHP reference: isset()
$numberofwelds = json_decode($_GET['numberofwelds'], true);
$numberofconwelds = json_decode($_GET['numberofconwelds'], true);
$mergedJson = array_merge(numberofwelds, numberofconwelds);
echo json_encode($mergedJson);
This should do it. It grabs the json, decodes and turns it in to an array (second parameter of json_decode set to true) and then combines them.

make php variable from plain text get file

currently i am using the below code to get a file from a site which tells me the current server status of a game server. the file is in plain text format and out puts the following depending on server status:
ouput:
{ "state": "online", "numonline": "185" }
or
{ "state": "offline" }
or
{ "state": "error" }
file get code:
<?php
$value=file_get_contents('http:/example.com/server_state.aspx');
echo $value;
?>
I would like to turn the 'state' and 'numonline' into their own variables so i could output them using a if, like:
<?php
$content=file_get_contents('http://example.com/server_state.aspx');
$state <--- what i dont know how to make
$online <--- what i dont know how to make
if ($state == online) {
echo "Server: $state , Online: $online";
} else {
echo "Server: Offline";
)
?>
but i have no idea how to turn the 'state' and 'numonline' from the plain text into a variable of their own ($state and $online), how would i go about doing this?
Your data is JSON. Use json_decode to parse it into a usable form:
$data = json_decode(file_get_contents('http:/example.com/server_state.aspx'));
if (!$data) {
die("Something went wrong when reading or parsing the data");
}
switch ($data->state) {
case 'online':
// e.g. echo $data->numonline
case 'offline':
// ...
}
Use json_decode function:
$value = '{ "state": "online", "numonline": "185" }';
$json = json_decode($value, true);
print_r($json);
if ($json['state'] == 'online') {
echo "Server: " . $json['state'] . " , Online: " . $json['numonline'];
} else {
echo "Server: Offline";
}
Output:
Array
(
[state] => online
[numonline] => 185
)
I would like to turn the 'state' and 'numonline' into their own variables:
Maybe you are looking for extract,
Example:
$value = '{ "state": "online", "numonline": "185" }';
$json = json_decode($value, true);
extract($json);
//now $state is 'online' and $numonline is 185

How to update (overwrite) a json file using php

[
{
"id" : 1,
"name" : "levin",
"description" : "some desc",
"size" : "100KG",
"actions" : {
"walking" : true,
"eating" : true
}
},
{
"id" : 2,
"name" : "clara",
"description" : "some desc",
"size" : "2000KG",
"actions" : {
"walking" : false,
"eating" : true
}
}
]
This is my person.json file. I like to update(overwrite) existing values. Am not found any useful questions regarding this.
i have a name "levin" i like to overwrite it to empty or "---". But its should only based through "id" .Following is my php code but its not working :(
public function api_put()
{
//print($this->put('id')); am getting here 2 value from other page
//print($this->put('action'));.
if($this->put('action') == "remove"){
$file = json_decode(file_get_contents("assets/json/person.json"));
$new_val = array();
$i = 0 ;
foreach ($file as $key => $value) {
if((string)$value->id == $this->put('id')) {
$data[] = (string)$value->name="--";(string)$value->description="--";
$new_val[$i] = $data;
$i++;
}
}
file_put_contents('assets/json/person.json', json_encode($new_val));
$message = array('id' => $this->put('id'), 'message' => 'Successfully updated!!');
$this->response($message, 200);
}
}
How can i overwrite a json value depend upon a particular id and with out change all other id data. am using codeigniter REST api. thanks in advance
A couple of things.
You don't need a $new_val array, you can just edit these resources in place.
PHP is weakly typed, so doing (string)$value->id == $this->put('id') is not necessary. The PHP engine will do this conversion for you.
Casting on the left side of the equal sign does nothing. (string)$value->description="--"; That statement has no effect.
This is poor form, in any programming language ... (string)$value->name="--";(string)$value->description="--"; Two statements doing two operations should be on two different lines.
General concept, your code should be easy to read. Space it out a little bit, let it breath. When you come back to your code years later you'll be glad that you did.
public function api_put()
{
if ($this->put('action') == 'remove')
{
$file = json_decode(file_get_contents('assets/json/person.json'));
foreach ($file as $key => $value)
{
if ($value->id == $this->put('id'))
{
$value->name = '--';
$value->description = '--';
}
}
file_put_contents('assets/json/person.json', json_encode($file));
$message = array('id' => $this->put('id'), 'message' => 'Successfully updated!!');
$this->response($message, 200);
}
}
There, isn't that much nicer?
I think you must need to loop like this to store all details
foreach ($file as $key => $value) {
if((string)$value->id == $this->put('id')) {
$value->name="--";(string)$value->description="--";
}
$new_val[$i] = $value;
$i++;
}

Append json messages when multiple json arrays are echo'd with jQuery

I have 2 classes that return a json encoded array if an error message is added to the $_error array:
Validate.class.php:
public function showResponse()
{
if(!empty($this->_error)) {
return json_encode($this->_error);
}
else {
return true;
}
}
UserTools.class.php:
public function showResponse()
{
if(!empty($this->_error)) {
return json_encode($this->_error);
}
else {
return true;
}
}
Then in ajax.php I check if either of those classes return true, if so a new user can be added by a User class, then the user class will return a success response, if they don't return true, the json encoded errors in either UserTools.class.php or Validate.class.php are returned by either of those classes:
ajax.php
if($validate->showResponse() === true && $user_tools->showResponse() === true) {
$user = new User($username, $password, $email);
$user->save();
echo $user->showResponse();
}
else {
echo $user_tools->showResponse();
echo $validate->showResponse();
}
Firebug shows that everything get's returned as expected, UserTools.class.php returns the usernameexists error and Validate.class.php returns the others:
{"error":{"usernameexists":"Username already taken"}}
{"error":{"password":"This field is required","password_again":"This field is required","email":"This field is required"}}
Yet I can't display either of those messages via jQuery, if I remove 'echo $user_tools->showResponse();' from 'else' in ajax.php, the error messages do get appended correctly, when I want to display both errors, nothing get's appended.
jQuery file:
if(msg.error) {
if(msg.error['usernameexists']) {
$('#msg-username').show().html('<p></p>').addClass('error');
$('#msg-username p').append(msg.error['username']);
}
if(msg.error['username']) {
$('#msg-username').show().html('<p></p>').addClass('error');
$('#msg-username p').append(msg.error['username']);
}
if(msg.error['password']) {
$('#msg-password').show().html('<p></p>').addClass('error');
$('#msg-password p').append(msg.error['password']);
}
if(msg.error['password_again']) {
$('#msg-password_again').show().html('<p></p>').addClass('error');
$('#msg-password_again p').append(msg.error['password_again']);
}
if(msg.error['email']) {
$('#msg-email').show().html('<p></p>').addClass('error');
$('#msg-email p').append(msg.error['email']);
}
}
The reason its not working is because there are 2 seperate json objects
One way is to combine them, for that put this in your ajax.php
if($validate->showResponse() === true && $user_tools->showResponse() === true) {
$user = new User($username, $password, $email);
$user->save();
echo $user->showResponse();
}else {
$r1 = $user_tools->showResponse();
$r2 = $validate->showResponse();
if($r1 !== true && $r2 !== true){
$r1 = json_decode($r1);
$r2 = json_decode($r2);
foreach($r2['error'] as $k => $v)
$r1['error'][$k] = $v;
$r1 = json_encode($r1);
}else if($r1 === true){
$r1 = $r2;
}
echo $r1;
}
Other easier way would be to return the error object itself instead of json_encoded one from Validate.class.php and UserTools.class.php and combine them in ajax.php then output the json_encoded string. this would save the 2 json_decode calls in the above code.
Your return string contains two objects and i think that is what (rightly) confuses the json parser. Try prepending a { and appending a } to the else output, and separate the two objects with a comma

Getting header response code

This is part of a PHP script I am putting together. Basically a domain ($domain1) is defined in a form and a different message is displayed, based on the response code from the server. However, I am having issues getting it to work. The 3 digit response code is all I am interested in.
Here is what I have so far:
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3);
foreach ($get_http_response_code as $gethead) {
if ($gethead == 200) {
echo "OKAY!";
} else {
echo "Nokay!";
}
}
}
$domain1 = 'http://google.com';
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3);
}
$get_http_response_code = get_http_response_code($domain1);
if ( $get_http_response_code == 200 ) {
echo "OKAY!";
} else {
echo "Nokay!";
}
If you have PHP 5.4.0+ you can use the http_response_code() function. Example:
var_dump(http_response_code()); // int(200)
Here is my solution for people who need send email when server down:
$url = 'http://www.example.com';
while(true) {
$strHeader = get_headers($url)[0];
$statusCode = substr($strHeader, 9, 3 );
if($statusCode != 200 ) {
echo 'Server down.';
// Send email
}
else {
echo 'oK';
}
sleep(30);
}
You directly returned so function wont execute further foreach condition which you written. Its always better to maintain two functions.
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3); //**Here you should not return**
foreach ($get_http_response_code as $gethead) {
if ($gethead == 200) {
echo "OKAY!";
} else {
echo "Nokay!";
}
}
}

Categories