This question already has answers here:
PHP function to build query string from array
(4 answers)
Closed 7 months ago.
I have an array of data comes from $_POST and I want to send them by curl to another page.
curl_setopt($s,CURLOPT_POST,true);
curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);
$this->_postFields must be an string like a=2&b=t right?
so if I want to send array of data with curl to another page I must turn array to query string right?
How should I do it with PHP?
♦I tried serialize() and unserialize() but thier format is not same as query string right?
so what should I do? (I need something like .serialize() in jQuery that work on array not FORM)
♦ And the destination path is not under my control and the $_POST in the destination should be as $_POST not as its base64 encoded so I can't use such codes.
$array = array(1,2,3);
$encoded = json_encode($array);
$decoded = json_decode($encoded);
print_r($decoded);
Any Ideas?
thanks in advance.
You can use http_build_query:
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields));
NOTICE
It looks beauty, but to use this approach be careful about url encoding .Look next:
$_POST["some value"]='value1'; // " " between
$_POST["next value"]='value2'; // " " between
$url = http_build_query($_POST);
echo $url;
// OUTPUT
some+value=value1&next+value=value2
Of course, after sending this $url we will not get expected variables from $_POST.
I think you can just do this:
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields));
If I understand, you can use json.
$array = array(1,2,3);
$encoded = json_encode($array);
$decoded = json_decode($encoded);
print_r($decoded); //Look to it
If is not it, can be it:
$array = array(1,2,3);
echo http_build_query($array); //Look to it
Related
I run the following PHP after I submit a form and get the output shown below:
$data = json_encode($_POST);
// output
{"First_Name":"Fred"}
How do I use PHP to just display the value 'Fred'?
I tried echo $data['First_Name']; but this is blank.
You no need to encode your incoming $_POST data.
Just say:
echo $_POST['First_Name'];
If you get a json data, decode it into an array:
$data = '{"First_Name":"Fred"}';
$decoded = json_decode($data, true);
echo $decoded['First_name'];
First of all, don't know why you use json_encode on PHP Array, and try to access it like it's an array - because after json_encode it's a string.
You have to use json_decode($data, true) and then you can access it like $data['First_Name'] or try to access it directly without json_encode() by $_POST['First_Name']
The json_decode() function is used to decode or convert a JSON object to a PHP object.And try to put the object to decode in another variable to avoid errors
<?php
$obj = '{"First_Name":"Fred"}';
$data = json_decode($obj);
echo ($data->First_Name);
?>
I have written a php script to decode a value using base64_decode() function. I want to store the parse the result into different variable. How do I achieve this??
$str = 'eyJ0eXAiO0IjoiNTAwMCIsInZhbGlkaXR5IjoiMzAifQjdubnmGHANidodnd';
$msisdn_64 = base64_decode($msisdn);
print_r($msisdn_64);
NB: For privacy sake the $str variable contains dummy value And this Does not use a token to decode
The code above outputs:
{"typ":"JWT","alg":"HS256"}{"sub":"456564685455","service":"000","created":20010809,"account_name":"Acct","iss":"Acft","exp":false,"amount":"000","validity":"30"}}�.'���A˕X=·&|�L�0�"����
I tried something like this $msisdn[1]->sub , $msisdn[0]->sub , and $msisdn->sub to the value in the second object but its not working. Please help
You can use the json_decode function
https://www.php.net/manual/en/function.json-decode.php
Example:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
$str = 'eyJ0eXAiO0IjoiNTAwMCIsInZhbGlkaXR5IjoiMzAifQjdubnmGHANidodnd';
$msisdn_64 = base64_decode($msisdn);
print_r($msisdn_64);
Here you need to decode the string again like below.
$str = 'eyJ0eXAiO0IjoiNTAwMCIsInZhbGlkaXR5IjoiMzAifQjdubnmGHANidodnd';
$msisdn_64 = json_decode(base64_decode($msisdn),true);
print_r($msisdn_64);
remember to put the parameter true in json_decode() method, because it returns the result as an associative array.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
Some fields save data in the following way in one of my database field:
{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}
(* Don't know how it's called so i wrote string in the title)
How can i access data in particular for every "value".
One way could be to explode for every field but is there a better way to do this?
$ask_for_price_variable = [value from field];
if ($ask_for_price_variable == 'YES') {
// Do something
}
EDIT: As i said i did not know how it was called "JSON" so i could not search for it. Thank you all for the answers.
It is json data. You can access it using json_decode in php
$json = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
$data = json_decode($json,true);
$ask_for_price_variable = $data['ask_for_price_en-GB'];
Use json_decode
$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
print_r(json_decode($str, true));
You need to study about JSON type and how to encode or decode to get the data.
try this
echo "<pre>";
print_r( json_decode('{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}'));
to view the result , click run or hit f9, here
try json_decode function
$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
$data = json_decode($str, true);
print_r($data);
output:
Array ( [per_meter_en-GB] => TEST_FOR_TEST [roll_40_en-GB] => [ask_for_price_en-GB] => YES )
here you can access required value from $data array eg. echo $data['per_meter_en-GB']; will output TEST_FOR_TEST
I insert data using json_decode to MySQL database like this:
["kevin","rode","shure"]
Now I need to convert using json_decode to php array like this:
$selected = array( 'kevin', 'rode', 'shure' );
how do can I convert this?
You can try this
$encoded = '["kevin","rode","shure"]';
$decoded = json_decode($encoded);// or directly pass the value here
print_r($decoded);
And you should get back the encoded array
Convert de data in a string, or get from $_GET or $_POST, it depends of you
$data = '["kevin","rode","shure"]';
$data = json_decode($data);
foreach($data as $name){
echo $name . "<br />";
}
and beware of double quotes
use json_decode with true param to force array output :
$selected = json_decode('["kevin","rode","shure"]', true);
NB : don't forget quotes and see documentation http://php.net/manual/en/function.json-decode.php
This is my json.
In php
$json = json_decode($finalAppData, true); // decode the JSON into an associative array
//suppose this is $link = ['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0];
This code is not working.
echo $json .$link."['screen']['menuHeader']";
produces output as
Array['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0]['screen']['menuHeader'].
but i want text value that can be seen if i use simply
echo $json['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0]['screen']['menuHeader'];
How can use index which is stored in variable to output data from json in php.
This should be similar to what you want:
$json = json_decode($finalAppData, true);
$link = "['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0]";
# Method #1
eval("echo \$json${link}['screen']['menuHeader'];");
# Method #2
$item = "\$json${link}";
eval("echo ${item}['screen']['menuHeader'];");
eval() takes a string of PHP code and interprets it. In this case, the nested keys are stored as a string in $link and then concatenated with a string that will be interpreted into the $json array resulting in a string of PHP code that will be sent to eval() to be interpreted.