1

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

6
  • 2
    Never ever pass json array through url, very bad Commented Jan 29, 2016 at 6:11
  • also the format you need to use is:-http://localhost/datas.php?data ='[{ 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}] ' Commented Jan 29, 2016 at 6:12
  • actually i got this url from an android app..how can i overcome this issue. Commented Jan 29, 2016 at 6:13
  • i formatted url like this. but its showing the same. Commented Jan 29, 2016 at 6:15
  • try: $json = json_encode($_GET['data]; var_dump($json) and remove the rest of your posted php code Commented Jan 29, 2016 at 6:19

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@user5060801 check above answer
0

Try like below.

$json = file_get_contents('url_here');

$obj = json_decode($json);

echo $obj->access_token;

1 Comment

@sunil kumar, there is no need of file content read. OP asking for url not any file data !
0

Atlast found solution for this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.