0

I have this url http://bisscloud.britain.edu.pk:8841/BISSJSon/JSonStudentsAll.aspx which contains data in the form of json array and i want to extract the data using php i tried using the file_get_contents

<?php
error_reporting(E_ALL);
$file = file_get_contents("http://bisscloud.britain.edu.pk:8841/BISSJSon/JSonStudentsAll.aspx");
$data = json_decode($file);
var_dump($data);

?>   

but it returns null i also tried using CURL but no result

6
  • 1
    Warning: file_get_contents(http://bisscloud.britain.edu.pk:8841/BISSJSon/JSonStudentsAll.aspx): failed to open stream: Operation timed out in php shell code on line 1... try using E_ERROR | E_WARNING | E_PARSE in error_reporting and you should see it Commented Jun 19, 2021 at 21:39
  • Thanks, this is what i got Warning: file_get_contents(bisscloud.britain.edu.pk:8841/BISSJSon/JSonStudentsAll.aspx): failed to open stream: Connection refused in /home/themessa/public_html/lms/api/json.php on line 4 Commented Jun 19, 2021 at 21:50
  • Hi @Omen Khalid, I try your example code, and I got some content. Commented Jun 19, 2021 at 21:59
  • 1
    Just tried to go to the URL and it seems to time out. So your problem has nothing to do with JSON, the URL simply doesn't work properly. Judging by the varying reports above, it's pretty unreliable Commented Jun 19, 2021 at 22:00
  • @cbrr09 what did you get exactly Commented Jun 19, 2021 at 23:29

1 Answer 1

2

The response is 6.9 MB, containing 9767 student records, which is probably why the response is timing out for most people, the server is probably having a hard time generating the response. If you do manage to get the response and try to decode it, json_decode will return null because it is unable to decode the JSON. If you use the JSON_THROW_ON_ERROR flag, or check the json_last_error, you will learn that there is a syntax error in the JSON.

The problem is that there are two records that contain double backslashes. The backslash is a special character in JSON, and a literal backslash must be escaped using... another backslash. So in these two elements, the \\ needs to be \\\\

{
      "StudentID": "10095",
      "RegistrationNo": "10078",
      "AdmissionDate": "01 Nov 2017",
      "StudentName": "Aaniya Tariq",
      "FatherName": "Tariq Shehzad",
      "Gender": "Female",
      "HomeAddress": "\\House No. 231-Block-H2, Johar Town",
      "HomePhone": "0324-4514622",
      "EmergencyPhone": "03064142718",
      "DateOfBirth": "24 Sep 2012",
      "ContactNo": "0324-4514622 ; 03064142718",
      "FamilyNo": "03064142718",
      "WebPassword": null,
      "CampusName": "Al-Muqeet Campus",
      "SessionYear": "2020-2021",
      "ClassTitle": "Level-III",
      "SectionTitle": "Green",
      "CampusID": "31",
      "SessionID": "12",
      "ClassID": "6",
      "SectionID": "1"
    },
    {
      "StudentID": "15929",
      "RegistrationNo": "15912",
      "AdmissionDate": "12 Oct 2020",
      "StudentName": "Fatima Tu Zahra \\ waseem Hashmi",
      "FatherName": "M. Waseem Hassan Hashmi",
      "Gender": "Female",
      "HomeAddress": "Punjab small Industries, Rawwal Road, Multan",
      "HomePhone": "N/A",
      "EmergencyPhone": "033152701010",
      "DateOfBirth": "16 Jan 2008",
      "ContactNo": "N/A ; 033152701010",
      "FamilyNo": "033152701010",
      "WebPassword": null,
      "CampusName": "Al-Awwal Campus",
      "SessionYear": "2020-2021",
      "ClassTitle": "Level-VIII",
      "SectionTitle": "Green",
      "CampusID": "9",
      "SessionID": "12",
      "ClassID": "11",
      "SectionID": "1"
    }

Fixing those two syntax errors allowed me to decode the JSON string.

$file = file_get_contents("http://bisscloud.britain.edu.pk:8841/BISSJSon/JSonStudentsAll.aspx");

try
{
    $data = json_decode($file, true, 512, JSON_THROW_ON_ERROR);
    echo sizeof($data['Students']) . PHP_EOL;
    var_dump($data);
}
catch (JsonException $e)
{
    echo 'Error code: '.$e->getCode().PHP_EOL;
    echo $e->getMessage().PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I removed the double backslashes and am still getting the failed to open stream: Connection refused error on file_get_contents(), is the problem with the url itself because when i run it, it returns valid json data
did you successfully decode the json string using the same url, if so then i think the connection refused problem is with my server can you confirm that ? @RobRuchte
Yes, I just ran the code I posted and was able to fetch and decode the response successfully.

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.