Since my host doesn't allow file_get_contents
I must use something else to do an API call.
I got this code to get the content of a website:
function get_contents_by_uri($uri) {
$uriElem = parse_url ( $uri );
$fp = @fsockopen ( $uriElem ['host'], 80, $errno, $errstr, 10 );
if (! $fp) {
throw new Exception ( "Could not create socket: '" . $errnstr . "' (" . $errno . ")." );
}
$request = "GET " . $uriElem ['path'] . (isset ( $uriElem ['query'] ) ? "?" . $uriElem ['query'] : "") . " HTTP/1.1
";
$request .= "Host: " . $uriElem ['host'] . "
";
$request .= "Connection: Close
";
fwrite ( $fp, $request );
$response = "";
while ( ! feof ( $fp ) ) {
$response .= fgets ( $fp, 128 );
}
fclose ( $fp );
// split headers from data
$responseSplit = explode ( "
", $response, 2 );
return $responseSplit [1];
}
This works fine for this simple call: http://dogechain.info/chain/Dogecoin/q/getblockcount
But it adds some data to calls containing JSON.
Example:
If I use get_contents_by_uri()
on http://data.bter.com/api/1/ticker/doge_btc, I don't get
{"result":"true","last":"0.00000188","high":"0.00000191","low":"0.00000185","avg":"0.00000188","sell":"0.00000189","buy":"0.00000188","vol_doge":82094227.628,"vol_btc":154.34039658}
but
b6 {"result":"true","last":"0.00000188","high":"0.00000191","low":"0.00000185","avg":"0.00000188","sell":"0.00000189","buy":"0.00000188","vol_doge":82094227.628,"vol_btc":154.34039658} 0
How do I get the sole JSON part of this string?