0

Hello So i have this call.

$preciobitcoin = curlCall('https://www.bitstamp.net/api/ticker/');

and i can perfectly get $preciobitcoin['last'].

but this one

$preciodolar = curlCall('https://s3.amazonaws.com/dolartoday/data.json');

return as a string instead of an array

this is the code of the function which is not working this is the code of the function which is not working

function curlCall($url, $params = null, $contentType = 'application/json', $options = array()) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 4);

if (!is_null($params) && !is_null($options['key']) && !is_null($options['sig'])) {
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: '.$contentType, 'key: '.$options['key'], 'sig: '.$options['sig']));
    //
} else if (!is_null($params) && !empty($params)) {
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: '.$contentType));
} else {
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: '.$contentType));
}

// Allow for custom requests
if (isset($options['custom_request']) && !empty($options['custom_request'])) {
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $options['custom_request']);
}

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; cryptoGlance ' . CURRENT_VERSION . '; PHP/' . phpversion() . ')');

$curlExec = curl_exec($curl);
if ($curlExec === false || curl_errno($curl)) {
    $data = array();
} else {
    $data = json_decode($curlExec, true);
}

if (empty($data)) {
    // return non-jsonfied data
    return $curlExec;
}

curl_close($curl);

return $data;

}

5
  • 1
    show how you parse the result in both cases by editing your post Commented Jul 18, 2015 at 13:24
  • Did you try to use json_decode to parse the json string to array? Commented Jul 18, 2015 at 13:25
  • yes, the variable becomes null.. Commented Jul 18, 2015 at 13:26
  • echo $preciodolar->USD->transferencia try this Commented Jul 18, 2015 at 13:26
  • It's wierd it's like the function does not recognize it as an json Commented Jul 18, 2015 at 13:35

2 Answers 2

1

Probably is an encoding issue. Here is an example that works correctly on my localhost.

$str = file_get_contents( 'https://s3.amazonaws.com/dolartoday/data.json' );
var_dump( mb_detect_encoding( $str ) );

$str = mb_convert_encoding( $str, "UTF-8" );
var_dump( mb_detect_encoding( $str ) );

var_dump( json_decode( $str ));
Sign up to request clarification or add additional context in comments.

Comments

0

a quick call to json_last_error() returns this

Malformed UTF-8 characters, possibly incorrectly encoded

possibly there are a few hidden bad chars (not utf8) in that file.

2 Comments

I was thinking that.. is there anything i could do on my side?
$text = iconv("utf-8", "utf-8//ignore", $text);

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.