0

I have a code which should fetch courseID from JSON response obtained , But currently it is not fetching anything . It shows me the entire response and not one . can someone help me with this .

JSON response obtained.

{
  "results": [
    {
      "CourseID": "ASPO",
      "Success": "Yes"
    }
  ]
}

i just want to retrieve the CourseID.

and below is my code

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://AbC:3939');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "ABC=AE");

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Authorization: Bearer hjhjhjhjhjhj338sj';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);


if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch) ;
}else{
    $result_value = json_decode($result);
   print_r($result_value->results[0]->CourseID);
}
curl_close($ch);
?>

2 Answers2

0

Note: this answers the original question (extract information from JSON response).

The top level element in your JSON string is an object with a single results key that holds everything. You aren't referencing such key anywhere in your code.

The name of the local PHP variable where you store the results is irrelevant, there isn't any automatic behaviour linked to that.

If you use json_decode() with the $associative flag you get JSON objects converted to PHP arrays (JSON arrays become PHP arrays no matter the flags). If unsure, just inspect your data with either a step debugger or e.g. var_dump($result_value):

array(1) {
  'results' =>
  array(1) {
    [0] =>
    array(2) {
      'CourseID' =>
      string(4) "ASPO"
      'Success' =>
      string(3) "Yes"
    }
  }
}

In php you can access array elements by specifying the key inside square brackets so:

var_dump($result_value['results'][0]['CourseID'], $result_value['results'][0]['Success']);
string(4) "ASPO"
string(3) "Yes"
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Kindly check the entire question, i have posted the entire CURL PHP file. Since am not getting what you guys said . –  Apr 27 '22 at 09:15
  • I think you've made it a different question (how to get response body using Curl). You are neither capturing Curl response nor returning it with `CURLOPT_RETURNTRANSFER`. – Álvaro González Apr 27 '22 at 09:58
  • got it . That was the reason –  Apr 27 '22 at 10:06
0

I have tried this with your shared result and got the COURSEID.

$resp = '{
  "results": [
    {
      "CourseID": "ASPO",
      "Success": "Yes"
    }
  ]
}';


$res = json_decode($resp);

print_r($res->results[0]->CourseID);
Sachin
  • 397
  • 4
  • 13
  • This works, but when i do the same thing in my code it just don't return anything –  Apr 27 '22 at 09:16
  • $result_value = json_decode($result, true); print_r($result_value); Could you please change a little in print result like above, and share the result ? – Sachin Apr 27 '22 at 09:22
  • Yup sure, just a min –  Apr 27 '22 at 09:34
  • {"results":[{"CourseID":"ASPO","Success":"True"}]}1 –  Apr 27 '22 at 09:41
  • Please try this: $result = // store your result in var $result i.e. '{"results":[{"CourseID":"ASPO","Success":"True"}]}'; $res = json_decode($result, true); print_r($res['results'][0]['CourseID']); – Sachin Apr 27 '22 at 09:46
  • one second, will update you –  Apr 27 '22 at 09:49
  • am still getting the entire JSON value as Result ->{"results":[{"CourseID":"ASPO","Success":"True"}]} –  Apr 27 '22 at 09:51
  • Please share what you are trying with output in all stages? – Sachin Apr 27 '22 at 09:55
  • i just want to get the value of CourseID as output. That's it –  Apr 27 '22 at 09:59
  • just want to check if courseID is ASPO, and allot some values to variable –  Apr 27 '22 at 10:00
  • Are you sure your output is {"results":[{"CourseID":"ASPO","Success":"True"}]} when you print $result_value = json_decode($result, true); print_r($result_value); Because it should give an array. – Sachin Apr 27 '22 at 10:02
  • yeah , it is showing the same output –  Apr 27 '22 at 10:02
  • {"results":[{"reCourseID":"ASPO","Success":"True"}]}1 –  Apr 27 '22 at 10:04
  • json_decode function should give an array but in your case it prints json, strange. And one more thing, what is 1 in the end of json output? – Sachin Apr 27 '22 at 10:06