user5060801
user5060801

Reputation: 77

json array in php url

I have a url like this:

http://localhost/datas.php?[{ id :27,latt:8.55699,ltd:76.882,tm:11:46:51}, { 
id :97,latt:8.55699,ltd:76.882,tm:11:46:52}, { id 
:31,latt:8.55699,ltd:76.882,tm:11:46:52}, { id 
:96,latt:8.55703,ltd:76.8815,tm:11:53:22}] 

I need to enter all these values to database using php

I tried many ways.but no way, its not working.

So far I done this.

$response = array();
$response["data"] = $array;
$json = json_encode($response);
var_dump($json);

but it shows data null.

please help me to solve this

Upvotes: 1

Views: 1465

Answers (3)

user5060801
user5060801

Reputation: 77

Atlast found solution for this.

$a = $_REQUEST['data'];
$bb = json_decode($a,true);
foreach ($bb as $res=>$value) {
$uid = $value["id"];
.
.
.
}

Upvotes: 0

Sunil Kumar Sain
Sunil Kumar Sain

Reputation: 105

Try like below.

$json = file_get_contents('url_here');

$obj = json_decode($json);

echo $obj->access_token;

Upvotes: 0

AnkiiG
AnkiiG

Reputation: 3488

Never ever pass json through URL, you can convert it into string and then decode it as below :

test.php

<?php 
$json = '[{
    "id": 27,
    "latt": 8.55699,
    "ltd": 76.882,
    "tm": "11: 46: 51"
}, {
    "id": 97,
    "latt": 8.55699,
    "ltd": 76.882,
    "tm": "11: 46: 52"
}, {
    "id": 31,
    "latt": 8.55699,
    "ltd": 76.882,
    "tm": "11: 46: 52"
}, {
    "id": 96,
    "latt": 8.55703,
    "ltd": 76.8815,
    "tm": "11: 53: 22"
}]';
echo 'http://localhost/test.php?data='.base64_encode($json);
?>

test1.php

<?php 
$getdata = $_GET['data'];
$cjson = json_decode(base64_decode($getdata));
print_r($cjson);
?>

Result you get will be :

Array
(
    [0] => stdClass Object
        (
            [id] => 27
            [latt] => 8.55699
            [ltd] => 76.882
            [tm] => 11: 46: 51
        )

    [1] => stdClass Object
        (
            [id] => 97
            [latt] => 8.55699
            [ltd] => 76.882
            [tm] => 11: 46: 52
        )

    [2] => stdClass Object
        (
            [id] => 31
            [latt] => 8.55699
            [ltd] => 76.882
            [tm] => 11: 46: 52
        )

    [3] => stdClass Object
        (
            [id] => 96
            [latt] => 8.55703
            [ltd] => 76.8815
            [tm] => 11: 53: 22
        )

)

Also the json is not in correct format. Please use validated json.

Upvotes: 1

Related Questions