How to read data from this json format - php

I am stuck on this. This is a json_encoded string by the Wordpress plugin and saved into database.
I want to read it from my own database query. I am getting null when tried with var_dump .
It has some properties of code which is creating problem, I think.
Below is the data where from i want to read usable data for my use. I am using PHP and Mysql.
a:3:{i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:3:"100";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:3:"200";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:8:"Infinite";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}}
I know this looks nasty but copy and try to decode it.

Ohh In Wordpress you can do as follows
$serialized = 'a:3:{i:0;s:5:"examp";i:1;s:6:"exampl";i:2;s:6:"examp2";}';
var_dump(unserialize($serialized));
Array
(
[0] => examp
[1] => exampl
[2] => examp2
)

Related

How to view a tinyblob object

I'm currently working with an existing database that is Mysql, and the system is built in php.
For whatever reason the builder of this system chose to store some parts of the data in blobs. One of them is a tiny blob.
In the database one of the records appears like this:
a:2:{i:0;s:3:"130";i:1;s:3:"182";}
This is viewable from the sql client I'm using. It says it's a TINYBLOB(255).
I need to be able to figure out the correct structure used to set this up so that I could build my part.
It appears to me as if I'm not seeing a "true" representation of what the data structure is.
I ran this on the php side:
public function types_get() {
$returnedTypes = $this->api->getReportTypes();
echo($returnedTypes);
$this->response($returnedTypes,REST_Controller::HTTP_OK);
}
It also produced this on the echo and response: a:2:{i:0;s:3:"130";i:1;s:3:"182";}
How would I be able to make it so I can see the true data as if it was a json string?
This data string has been created with the serialize() function. You can convert it back to a native array with the matching unserialize() function:
$string = 'a:2:{i:0;s:3:"130";i:1;s:3:"182";}';
$data = unserialize($string);
print_r($data);
Output:
Array(
[0] = 130
[1] = 182
)

How to convert an array in PHP into a valid JSON request and send it to web service URL in following scenario?

I've got following array titled $val in PHP :
Array
(
[page_id] => 208
[invite_emails] =>
[invite] => Array
(
[0] => 970
[1] => 991
[2] => 992
)
)
I only want to convert the above array into a valid JSON request and send it to some web service URL.
How should I do it? Please help me.
Thanks in advance.
Your question is rather broad, but to convert a php array into a JSON object is very easy.
$jsonString = json_encode($array);
As for sending it to the URL, this question contains some good info.
Or if you want to use curl, this resource is pretty good.
As others have posted, use the following to json encode your array:
json_encode($val);
Now, your problem is sending this to your web service via parameter. The way I generally send complex parameters via GET parameters is by base64 encoding.
$param = base64_encode(json_encode($val));
Now you can send $param just like any other parameter. Take a look at Guzzle for making HTTP requests from PHP.

display data from an array in json form

i got an array from echo $posts
Array
(
[0] => Array
(
[id] => 14
[name] => اسطنبوليه
)
)
i have this array (part of the main array)and now i wish to convert it to json form. however i am not able to do so, i tried to convert the data through
echo json_encode ($posts);
but instead of original data i am getting an output u0627u0633u0637u0646u0628u0648u0644u064au0647n
can anyone tell how i can get the correct form
Please try echo json_encode($posts,JSON_UNESCAPED_UNICODE) (php 5.4 and above)
2 things. Firstly view source of the output in the browser and you should see it as a JSON encoded string rather than the interpreted version.
Secondly it looks like there is some character encoding issues as the string you're getting back is unicode. Make sure you have the encoding set right on your server and browser.

cakephp sending encrypted data to library from Controller

While working with the cakephp, I found an issue mentioned below.
Fetched the encrypted field info from DB (encrypted using Security::rijndael)
Passed this whole data as an array format to the custom Library(Own created lib).
When i echoed the data in lib as well in controller I amazed to see the result. The value (encrypted one) are showing blank in the lib. Is I missed anything in codding? I searched on google but didn't get the satisfactory answer, Please help me out. Your help will really be appreciated.
Here is result i am getting in controller and Library respectively
Array
(
[0] => Array
(
[value] => s�i�(�RTf���cBЉF� | �r�n#ô�
)
)
Array
(
[0] => Array
(
[value] =>
)
)
Check your character encoding; a place I worked at ran into a similar issue and it was due to our db trying to encode characters it did not support. UTF-8 generic is a, well, generic encoding type.

dump xml string verbatim to mysql db

I need to dump an xml string (coming into a web service as a POST param), directly into a Mysql DB. The column into which I am writing is currently of type 'text', though I have tried blob as well.
Right now, when I save the string and retrieve it later on using sql, it comes back in a serialized like format, like this:
a:1:{s:14:"<?xml_encoding";s:1502:"UTF-8?><record>
<nodes></nodes>
</record>";}
Whereas, I need to parse that xml as a simplexml object, and hence need it to be better formed.
This is what I am doing codewise:
Accept Post Param in web service
Serialize and store it in DB using doctrine ORM, kind of like
$record->xml_rec = serialize($_POST)
Retrieve it and echo it.
Oddly enough, if I unserialize and echo is upon retrial, I get an array. The array looks like this upon print_f
Array
(
[<?xml_encoding] => UTF-8?><record>
<nodes></nodes>
</record>
)
Still not parse-able by simplexml.
Is this a mysql storage problem or am I doing something wrong with the POST params? I've tried htmlspecialchars to save the string in the db, but similar problems remain.
any help is appreciated, thanks
What you have is a "serialized" php array that you can unserialize by using ... PHP's unserialize function.

Categories