Asked  7 Months ago    Answers:  5   Viewed   30 times

Solr returns response in following JSON format.



What will be the simple way to read student_id, student_name using PHP?



Use $obj = json_decode($yourJSONString); to convert it to an object.

Then use foreach($obj->response->docs as $doc) to iterate over the "docs".

You can then access the fields using $doc->student_id and $doc->student_name[0].

Wednesday, March 31, 2021
answered 7 Months ago

You have empty $_POST. If your web-server wants see data in json-format you need to read the raw input and then parse it with JSON decode.

You need something like that:

$json = file_get_contents('php://input');
$obj = json_decode($json);

Also you have wrong code for testing JSON-communication...

CURLOPT_POSTFIELDS tells curl to encode your parameters as application/x-www-form-urlencoded. You need JSON-string here.


Your php code for test page should be like that:

$data_string = json_encode($data);

$ch = curl_init('http://webservice.local/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))

$result = curl_exec($ch);
$result = json_decode($result);

Also on your web-service page you should remove one of the lines header('Content-type: application/json');. It must be called only once.

Wednesday, March 31, 2021
answered 7 Months ago

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle ", which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('"', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
answered 7 Months ago

I answered a similar question here: Sencha seems to not like Rails' json. It is specific to Rails but the concept still applies.

Essentially James Pearce is correct. What you are returning needs to be wrapped in a tag and the callback function. This will insert the code on your page and run the script, which has the effect of calling the function you provide.

$response = "<script type='text/javascript'>";
$response .= $_GET['callback'] . "(" . json_encode($row) . ")";
$response .= "</script>";
Wednesday, March 31, 2021
answered 7 Months ago

Finally I Solve The Problem. Here is My Solution

        $jsonObject = file_get_contents("");
    $jsonres = json_decode($jsonObject,true);               
        $val = $jsonres['Data'];      
$phpArray = json_decode($val, true);
foreach ($phpArray as $key => $value) {
    foreach ($value as $k => $v) { 
        if($k === 'CurrencyDescription'){ 

    echo '<option value=' . $curName. '>' . $curName. '</option>'; 

Friday, May 28, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :