2

I am trying to convert a JSON array to a PHP array, and the boolean values are being lost. I think I need to loop round using foreach() and cast the items as they are placed in to the new array, but I can't work out how to do it:

header ('Content-Type: application/json');
function return_json ($url) {
    // Create a new cURL instance
    $ch = curl_init ();
    if (!$ch) {
        die ('Could not find the cURL module');
    }

    // Set URL and other appropriate options
    curl_setopt ($ch, CURLOPT_URL, $url);

    // Set the encoding
    curl_setopt ($ch, CURLOPT_ENCODING, '');

    // Return the response header from stackapps.
    // curl_setopt ($ch, CURLOPT_HEADER, true);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

    // Grab URL and pass it to the browser.
    $result = curl_exec ($ch);
    curl_close ($ch);
    return $result;
}

$url = "https://api.stackexchange.com/2.2/tags?pagesize=14&order=desc&sort=popular&site=stackoverflow";
$html = return_json ($url);
if (!$html) {
    die ('request failed');
}
else if ($html) {
    $json_array = json_decode ($html, true);
    print_r ($json_array);
    // print_r ($html);
}

print_r($html) returns the regular JSON response fine.

1
  • 1
    can you post the output ; print_r($html) returns the regular JSON response fine; your code looks fine
    – JMP
    Commented Mar 21, 2015 at 4:08

0

You must log in to answer this question.

Browse other questions tagged .