I want to decode my user id(already encoded ) using vinkla hashids
$user_id= "1"; // I encoded this
$en_user_id = Hashids::encode($user_id); // I got 'ND'
$dec_user_id = Hashids::decode($en_user_id); // It is not decoding
I want to decode ND to get original ID
when I try to decode I got this
ErrorException
htmlspecialchars() expects parameter 1 to be string, array given
Related
Trying to send arraylist data to server using php as bridge from Android (using Volley), and successfully getting data into database table (and also getting response as submit, I mentioned in php) in Android
So everything looks perfect, if we talk about db connection, storage and response, but one unusal issue always getting 2 in table field, instead the original value
::::imageDataInString::::: {"b":"2020-01-01_00:01:11.jpg"}
::::imageDataInString::::: {"b":"2020-01-01_00:01:21.jpg"}
::::imageDataInString::::: {"b":"2020-01-01_00:01:31.jpg"}
::::Response::::: <br />
<b>Warning</b>: Illegal string offset 'image_name' in <b>C:\xampp\htdocs\test\send_data.php</b> on line <b>15</b><br />
<br />
submit
Here is the foreach, I'm using in php to upload all the data available in a list
$content = $_POST['my_images'];
$json = json_decode($content, true);
foreach ($json as $key => $value) {
$image_name = $value["image_name"];
}
android code
ImageData imageData = new ImageData();
imageData.setImageName(array[0]);
String imageDataInString = new Gson().toJson(imageData);
Log.d("::::imageDataInString::::", imageDataInString);
....
params.put("my_images", imageDataInString);
in my csv, available in raw folder of res in Android, I have these data:
2020-01-01_00:01:11.jpg
2020-01-01_00:01:21.jpg
2020-01-01_00:01:31.jpg
There are a few points to note:
GSON converts the variable names of the class (ImageData) as keys and appends the values, to form the JSON. Hence, you need to use the same variable names, as you have used in the ImageData class to fetch the data, in your PHP Code.
ImageData imageData = new ImageData();
imageData.setImageName(array[0]);
String imageDataInString = new Gson().toJson(imageData);
Hence, the array key should be array["ImageName"]
In your PHP code, the second line, where you are JSON decoding the $content variable, the variable $json is already converted in an Array. You don't need to iterate and fetch corresponding keys.
Hence, you should do something like this:
$content = $_POST['my_images'];
$json = json_decode($content, true);
$image_name = $json["ImageName"];
With this, the variable $image_name will hold the value which you are looking for.
i have a device that send POST data to my server.
so print_r($_POST) is empty, i can see the data only when i run this
$content = file_get_contents('php://input');
var_dump($content);
//or i can use: print_r($content);
i save those to a file and result are some json and BINARY DATA (CHECK IMAGE)
if i add code like this json_decode($content,true); i dont see anything
so how can i decode binary or what can i do to decode the json and also see what data is send in binary?
If you want to decode binary data inPHP, try the following:
<?php
$binarydata = "\x04\x00\xa0\x00";
$data = unpack('C*', $binarydata);
var_dump($data);
output:
array (size=4)
1 => int 4
2 => int 0
3 => int 160
4 => int 0
Load your contents from file_get_contents('php://input') to $binarydata and you will get an array of values. You can then apply some logic to extract JSON string and process it.
I keep getting the error "Warning: Illegal string offset 'code' in /path/ on line 17". I have tried many things but don't know what the problem is. Here is my code:
require_once 'unirest-php-master/src/Unirest.php';
$response = Unirest\Request::post("https://andruxnet-random-famous-
quotes.p.mashape.com/?cat=movies",
array(
"X-Mashape-Key" => "key",
"Content-Type" => "application/x-www-form-urlencoded",
"Accept" => "application/json"
)
);
$encoded = json_encode($response,true);
echo $encoded['code'];
This code is being used to get information from the api and it is successfully getting the information but when I try to access an object in the array I get an error.
I have also tried to use json_decode instead of encode but that gives me an error about parameter 1 needing to be a string
Edit: line 17 is this line: echo $encoded['code'];
From your code, it seems like you want to print the response obtained from API (in JSON format). So, you should be doing json_decode here.
var_dump($response); // Check response obtained from API
$encoded = json_decode($response,true); // Change to json_decode
echo "<pre>"; print_r($encoded); // Debug decoded array
echo $encoded['code']; // Check data
I have resolved the issue. The first thing I did wrong was try to decode or encode it, I didn't need to do either. I was also using ["code"] rather than ->code
I have a JSON-String like
{"aaa":"foo", "bbb":"bar", "ccc":"hello", "ddd":"world"}
Actually I recive this string via $_GET. It is base64 encoded and if I decode it I have this string.
So further is want to use this in PHP as an object. For this is do
$data = json_decode( base64_decode( $_GET['data'] ) );
but $data is NULL all the time. If I do
echo base64_decode( $_GET['data'] );
Then the valid JSON-String is printed as expacted.
What am I doing wrong ?
I have tried to call the base64_encode before but same result...
Check json_last_error() and see what error the JSON parser encountered. If you encounter 5 then it is very likely that your JSON data contains unencoded UTF-8 sequences. You should always use a JSON-encoding library for handling data export to json.
See http://php.net/manual/en/function.json-last-error.php for a list of what errors you can handle with json_last_error (There is a INT to definition name table in the user comments)
0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8
Sending a image to mysql using android, json and php
I was able to get my bitmap converted to a properly formatted string using
converting-images-to-json-objects
Here is the code in android
JSONObject values = new JSONObject();
values.put(KEY_CONTRACTUUID, con.UUID);
...
if (con._sig != null) {
String encodedImage = getStringFromBitmap(con._sig);
values.put(KEY_CONTRACTSIGIMAGE, encodedImage);
private static String getStringFromBitmap(Bitmap bitmapPicture) {
/*
* This functions converts Bitmap picture to a string which can be
* JSONified.
*/
final int COMPRESSION_QUALITY = 100;
String encodedImage;
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}
now that it is in base64 and a string I need to retrieve it properly to place in my BLOB in mysql
I am not using namevalue pairs or any of that nonsense - simply send it as json and get the json string like so:
$json = json_decode($HTTP_RAW_POST_DATA,true);
echo var_dump(HTTP_RAW_POST_DATA);
...
$varsigimage = $json['sigimage'];
$formatedJSONimage = "{'sigimage': '$varsigimage'}";
var_dump($formatedJSONimage);
$sigImagedecoded = json_decode($formatedJSONimage);
var_dump($sigImagedecoded);
i need to call json_decode on the image to get it out of 64bit to place in the blob correct?
However to do this I need to use the function json_decode, but json_decode assumes I will give it a JSONObject, and since I have many more objects in my $json object, i need to recreate a single JSON object with just the image inside of it, and pass that to the json_decode
but it retuns json_error of type SYNTAX
What am I doing wrong, What is the correct approach of converting the base64 string to a blob?
and yes, I am going to have the same question on getting it out of the blob back to a base64 string
json_decode parses a JSON string and returns an associative array, mimicking the key/value pairs in the JSON string.
It seems like you are missing another step: you need to decode the base64-encoded image string back to a bitmap. e.g. in your code:
$json = json_decode($HTTP_RAW_POST_DATA,true);
echo var_dump(HTTP_RAW_POST_DATA);
...
$varsigimage = $json['sigimage'];
$image_bitmap = base64_decode($varsigimage); // decode the string back to binary
You should now be able to save $image_bitmap as a BLOB in your database.