2

Hi i'm trying to get the data from a JSON file from a url using Curl and PHP with this code:

<?php
    //  Initiate curl
    $url = "https://panel.virtualcenter360.es/home/api/getdatasample?dateIni=1376524800&dateEnd=1381795200";
    $ch = curl_init($url);
    // Set the url
    curl_setopt($ch, CURLOPT_URL,$url);
    // Execute
    $result=curl_exec($ch);

    $res = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($result));

    var_dump(json_decode($res, true));

        switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}
    curl_close($ch);
?>

I'm getting the No errors message, but a NULL result. What do you think that could be the problem?

Maybe I need to configure some parameters from Curl?

Thanks!

3 Answers 3

1

if you want to work with curl you have to do this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

CURLOPT_SSL_VERIFYPEER
FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.

http://php.net/manual/en/function.curl-setopt.php

Sign up to request clarification or add additional context in comments.

Comments

1

If all you need to do is fetch some json info, I'd suggest you use file_get_contents() instead of the cURL functions.

3 Comments

I have also tried with file_get_contents() like: $result = file_get_contents('panel.virtualcenter360.es/home/api/…); var_dump(json_decode($result, true)); But I'm getting errors.
$result = file_get_contents('https://panel.virtualcenter360.es/home/api/getdatasample?dateIni=1376524800&dateEnd=1381795200'); var_dump(json_decode($result, true)); works fine for me. I suggest you try this with Advanced REST Client for chrome or some other tool to test.
Hi i have tried with file_gets_contents in a Online server and It works. I think the problem it's when tried from a wamp localhost server. Thanks!
0

In Codeigniter application/libraries folder

header('Content-Length: ' . strlen($output)); //Comment line no. 267 of REST_Controller.php

Comments

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.