1

I am very new to the PHP, I am coming from the background of .net. I am trying to make web service which will take parameter from URL and process business logic then output it in jSON.

This is my code

<?php
    $data = '';
    $Site = $_GET['COUNTRY_SITE'];
    $Language = $_GET['LANGUAGE'];

    $data = "{\"Site\":" . "\"" . $Site . "\"" . ",\"Language\":" . "\"" . $Language . "\"" . "}";
    //header('Content-type: application/json');
    //echo $data;
    $x =  json_decode($data,true);
    var_dump($x);

?>

I am getting below output

array(2) {
  ["Site"]=>
  string(5) "India"
  ["Language"]=>
  string(2) "GB"
}

Why I am not getting output like this

{
"Site":"India",
"Language":"GB"
}

Can somebody help me and do explain if possible

5 Answers 5

1

You are manually creating a JSON and then decode it. The output can be expected, in fact. I think you should use PHP tools to convert your object to JSON:

<?php
    $data = '';
    $Site = $_GET['COUNTRY_SITE'];
    $Language = $_GET['LANGUAGE'];

    //Associative array
    $data = array("Site" => $Site, "Language" => $Language);    

    $x =  json_encode($data, JSON_PRETTY_PRINT);
    var_dump($x);

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

Comments

1

Replace var_dump($x); to echo $x = json_encode(json_decode($data,true),JSON_PRETTY_PRINT);

Notes:

  1. json_decode($data,true) This will output an array.
  2. json_encode(json_decode($data,true),JSON_PRETTY_PRINT); This will change array into json format in pretty view

Output:

{
    "Site": "s",
    "Language": "ss"
}

Comments

1

What you're echoing out is a php array based on the JSON string you built, $data.

However you should reverse your flow and build a php array and then use json_encode() to build the string for you. This will ensure the json is always formatted correctly.

<?php
$data = [
  'Site' => $_GET['COUNTRY_SITE'],
  'Language' => $_GET['LANGUAGE']
];

header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);

The JSON_PRETTY_PRINT option will format it with spaces so that it's more pleasant to read but you can remove it to condense the output.

Comments

1
<?php
    $data = '';
    $Site = $_GET['COUNTRY_SITE'];
    $Language = $_GET['LANGUAGE'];

    $data = "{\"Site\":" . "\"" . $Site . "\"" . ",\"Language\":" . "\"" . $Language . "\"" . "}";
    //header('Content-type: application/json');
    //echo $data;
    $x =  json_encode(json_decode($data,true),JSON_PRETTY_PRINT);
    var_dump($x);

?>

If you want dynamic JSON result:

<?php
echo json_encode($_GET,JSON_PRETTY_PRINT);
?>

Comments

1

Try this code

<?php
    $data = '';
    $Site = $_GET['COUNTRY_SITE'];
    $Language = $_GET['LANGUAGE'];
    $data =array('Site'=>$Site,'Language'=>$Language);

    header('Content-type: application/json');

    $x =  json_encode($data);
    var_dump($x);

?>

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.