0

I am sending request to remote server and it is sending plain text JSON type response! I tried to take that response in php variable and tried for json_decode but it always returns null value!

<?php

function removefunction($data){
checkagain:
$functionposition=stripos($data,"function()");
if($functionposition){
$subdata= substr($data, $functionposition);
$functiondata=substr($data, $functionposition,stripos($subdata,"}")+1);

$endoffunction=stripos($subdata,"}");
$endoffunction=$endoffunction+$functionposition;

$questionmarkpos=stripos($functiondata,'?"');
$colonpos=stripos($functiondata,'":"');
$realvalue=substr($functiondata, $questionmarkpos+2,$colonpos-$questionmarkpos-2);
$data=str_ireplace($functiondata,"\"$realvalue\"",$data);
goto checkagain;
}
return $data;
}

$json=<<<EOT
obj1431027525490 = { trains: [ { trainNo: "12392", startDate: "6 May 2015", trainName: "SHRAMJEEVI EXPRESS", trnName:function(){return _LANG=="en-us"?"SHRAMJEEVI EXPRESS":""}, divertedFrom: "NDLS", divertedTo: "GZB", trainSrc: "NDLS", trainDstn: "RGD", trainType: "SUPERFAST" }, { trainNo: "13162", startDate: "7 May 2015", trainName: "BLGT-KOLKATA EXP.", trnName:function(){return _LANG=="en-us"?"BLGT-KOLKATA EXP.":""}, divertedFrom: "NFK", divertedTo: "KOAA", trainSrc: "BLGT", trainDstn: "KOAA", trainType: "MAIL_EXP" }] }; 
EOT;
$json= substr($json, 19);
$json=substr_replace($json, "", -2);
echo $json."<br/><br/><br/>".PHP_EOL.PHP_EOL.PHP_EOL;
$json=removefunction($json);
$json = preg_replace('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/', '"$1"', $json);
echo $json;
$contents = utf8_encode($json); 
$obj = json_decode($json,true);
var_dump($obj);

?>

Issue: preg_replace adds quote between already quoted text!

PS: This is how i want to print the data into table via php array https://i.stack.imgur.com/Pf0Ek.png

You can use Diverted Train response from original website in above screenshot!

ABC
  • 1
  • 1
  • 1
    the pasted response is not valid json. Thats the reason for returning null. There are no quotes and there are inline functions. – steven May 08 '15 at 05:04
  • yeah, even I felt that there are no quotes. But then how is http://enquiry.indianrail.gov.in/ntes/ showing the response? I tried code from original site (javascript not php) http://pastebin.com/usBpmFKv – ABC May 08 '15 at 05:09
  • the `obj1431027525490 = ` should not be a part of your json definition. And I don't belive that the inline functions should work in PHP. – steven May 08 '15 at 05:09
  • they execute it in javascript. As you can see in the inline functions, you need at least the `_LANG` variable to be defined locally. This is pure JS. This will not work in php. – steven May 08 '15 at 05:11
  • we can remove obj1431027525490 = by substr! Any idea how to add quotes to remaining part? and also use EN value of that JS function – ABC May 08 '15 at 05:12
  • I would not do it in php. If you grab it with ajax in javascript then it should work if you define _LANG locally. – steven May 08 '15 at 05:19
  • I want to save the values in database so PHP is must! :'( – ABC May 08 '15 at 05:23
  • so grab it with ajax in JS, parse it and post it with ajax in JS to your PHP as valid json. – steven May 08 '15 at 05:28
  • User of my site onclick button would be able to run the php code which would get these json-type value from that external server and covert it into php and then store it into mysql db and user would be returned value from mysql db! So cant post it with ajax JS from user side as it would be security flaw without many validations – ABC May 08 '15 at 05:37

3 Answers3

1

I should to tell you basic syntax to convert json to php variable. keep in mind, This is onli work with utf-8 encoding

// let the example yo be clear to understand

$json = {"foo-bar" : 1234};

$obj = json_decode($json);

print $obj -> {'foo-bar'}; // 1234

If you want to get it as php var

$var = $obj->{'foo-bar'}; // the values keep 1234

It turn json to php array assoc

You cannot use json to get in php var if you use json like

$json = {'foo' : '1234'};  // return null
$json = {foo : 1234}; // Null
$json = {foo: "1234"}; 

The example maybe valid in javascript, but not in json.

Dev1410
  • 43
  • 3
  • So dude got any idea? I tried this also: http://stackoverflow.com/questions/8815586/convert-invalid-json-into-valid-json – ABC May 08 '15 at 05:23
  • If it there some double quotes, will converted as string object, they tell the true, any question ? – Dev1410 May 08 '15 at 05:25
0

I was able to parse it with the following modifications:

  • removed the obj1431027525490 =
  • removed the ; at the end
  • removed the inline functions with key trnName
  • sourrounded all keys with double quotes

working example:

<?php
$json=<<<EOT
{ "trains": [ { "trainNo": "12392", "startDate": "6 May 2015", "trainName": "SHRAMJEEVI EXPRESS", "divertedFrom": "NDLS", "divertedTo": "GZB", "trainSrc": "NDLS", "trainDstn": "RGD", "trainType": "SUPERFAST" }, { "trainNo": "13162", "startDate": "7 May 2015", "trainName": "BLGT-KOLKATA EXP.", "divertedFrom": "NFK", "divertedTo": "KOAA", "trainSrc": "BLGT", "trainDstn": "KOAA", "trainType": "MAIL_EXP" }] }
EOT;

$contents = utf8_encode($json); 
$obj = json_decode($contents,true);
var_dump($obj);
steven
  • 4,868
  • 2
  • 28
  • 58
  • Yeah, I did it too! But how to remove inline function and surround all key without human intervention! I am trying stringfyjs! Have a look t o it – ABC May 08 '15 at 05:30
  • To Add double quotes: $json = preg_replace('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/', '"$1"', $json); – ABC May 08 '15 at 05:41
  • I made the php-function which would replace the javascript function with the value! Now issue left is of adding quotes! – ABC May 08 '15 at 06:27
0
<?php

function removefunction($data){
    checkagain:
$functionposition=stripos($data,"function()");
    if($functionposition){
    $subdata= substr($data, $functionposition);
    $functiondata=substr($data, $functionposition,stripos($subdata,"}")+1);

    $endoffunction=stripos($subdata,"}");
    $endoffunction=$endoffunction+$functionposition;

    $questionmarkpos=stripos($functiondata,'?"');
    $colonpos=stripos($functiondata,'":"');
    $realvalue=substr($functiondata, $questionmarkpos+2,$colonpos-$questionmarkpos-2);
    $data=str_ireplace($functiondata," \"$realvalue\"",$data);
    goto checkagain;
    }
    return $data;
}
function json_fix_quotes ($string){
    //$string = str_replace("{",'{"',$string);
    $string = str_replace(":'",'":"',$string);
    $string = str_replace(': ','": ',$string);
    $string = str_replace(", ",', "',$string);
    $string = str_replace("',",'","',$string);
    $string= str_replace('{" ','{"',$string);
    $string= str_replace(', "{',', {',$string);
    $string = str_replace("{ ",'{"',$string);
    return $string;
}

$json=<<<EOT
obj1431027525490 = { trains: [ { trainNo: "12392", startDate: "6 May 2015", trainName: "SHRAMJEEVI EXPRESS", trnName:function(){return _LANG=="en-us"?"SHRAMJEEVI EXPRESS":""}, divertedFrom: "NDLS", divertedTo: "GZB", trainSrc: "NDLS", trainDstn: "RGD", trainType: "SUPERFAST" }, { trainNo: "13162", startDate: "7 May 2015", trainName: "BLGT-KOLKATA EXP.", trnName:function(){return _LANG=="en-us"?"BLGT-KOLKATA EXP.":""}, divertedFrom: "NFK", divertedTo: "KOAA", trainSrc: "BLGT", trainDstn: "KOAA", trainType: "MAIL_EXP" }] }; 
EOT;
$json= substr($json, 19);
$json=substr_replace($json, "", -2);
echo $json."<br/><br/><br/>".PHP_EOL.PHP_EOL.PHP_EOL;
$json=removefunction($json);
$json = json_fix_quotes($json);
echo $json;
$contents = utf8_encode($json); 
$obj = json_decode($json,true);
var_dump($obj);

?>
ABC
  • 1
  • 1