Unable to pull orderbook, bitfinex api - php

I am experimenting with the bitfinex API.
I program in PhP and all the docs are JS or Ruby (I really need to learn more Ruby).
Bitfinex API docs #orderbook
I can pull user info, but I am unable to pull the orderbook
code:
<?php
function bitfinex_query($path, array $req = Array())
{
global $config;
// API settings, add your Key and Secret at here
$key = "xxxxxxxxx";
$secret = "xxxxxxxx";
// generate a nonce to avoid problems with 32bits systems
$mt = explode(' ', microtime());
$req['request'] = "/v1".$path;
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
// generate the POST data string
$post_data = base64_encode(json_encode($req));
$sign = hash_hmac('sha384', $post_data, $secret);
// generate the extra headers
$headers = array(
'X-BFX-APIKEY: '.$key,
'X-BFX-PAYLOAD: '.$post_data,
'X-BFX-SIGNATURE: '.$sign,
);
// curl handle (initialize if required)
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; Bter PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
);
}
curl_setopt($ch, CURLOPT_URL, 'https://api.bitfinex.com/v1'.$path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
//echo $res;
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data: '.$res);
return $dec;
}
//this works
$api_name = '/orders';
$openorders = bitfinex_query($api_name);
var_dump($openorders);
//broken
$api_name = '/book/BTCUSD';
$orderbook = bitfinex_query($api_name);
//$orderbook = bitfinex_query($api_name, array("limit_asks"=> 1, "group"=> 0));
?>
output:
array(1) {
[0]=>
array(16) {
["id"]=>
int(880337054)
["symbol"]=>
string(6) "btcusd"
["exchange"]=>
NULL
["price"]=>
string(6) "759.02"
["avg_execution_price"]=>
string(3) "0.0"
["side"]=>
string(4) "sell"
["type"]=>
string(14) "exchange limit"
["timestamp"]=>
string(12) "1467544183.0"
["is_live"]=>
bool(true)
["is_cancelled"]=>
bool(false)
["is_hidden"]=>
bool(false)
["oco_order"]=>
NULL
["was_forced"]=>
bool(false)
["original_amount"]=>
string(4) "0.05"
["remaining_amount"]=>
string(4) "0.05"
["executed_amount"]=>
string(3) "0.0"
}
}
PHP Fatal error: Uncaught exception 'Exception' with message 'Invalid data: <!DOCTYPE html>
<!--[if IE 8]>
<html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="description"
content="The largest and most advanced cryptocurrencies exchange">
<meta name="keywords"
content="bitcoin,exchange,bitcoin exchange,litecoin,ethereum,margin,trade">
<meta property="og:title" content="Bitfinex">
<meta property="og:description"
content="The largest and most advanced cryptocurrencies exchange">
<meta property="og:image" content="https://bitfinex.com/assets/bfx-stacked.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="#bitfinex">
<meta name="twitter:title" content="Bitfinex">
<meta name="twitter:description"
content="The largest and most advanced cryptocurrencies exchange">
<meta name="twitter:image" con in /home/bitfinex/buycoinz.php on line 49
Fatal error: Uncaught exception 'Exception' with message 'Invalid data: <!DOCTYPE html>
<!--[if IE 8]>
<html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="description"
content="The largest and most advanced cryptocurrencies exchange">
<meta name="keywords"
content="bitcoin,exchange,bitcoin exchange,litecoin,ethereum,margin,trade">
<meta property="og:title" content="Bitfinex">
<meta property="og:description"
content="The largest and most advanced cryptocurrencies exchange">
<meta property="og:image" content="https://bitfinex.com/assets/bfx-stacked.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="#bitfinex">
<meta name="twitter:title" content="Bitfinex">
<meta name="twitter:description"
content="The largest and most advanced cryptocurrencies exchange">
<meta name="twitter:image" con in /home/bitfinex/buycoinz.php on line 49
How do I correctly access the bitfinex api orderbook?

It doesn't look like you are hitting the right endpoint.
Also the orderbook is public. Your example is for authenticated endpoints, like making a trade. You don't need to SHA384 or base64 anything for the public endpoints, you can just do a simple curl or even a file_get_contents.
Here's the endpoint for the orderbook: https://api.bitfinex.com/v1/book/BTCUSD
Now you can do what you'd like with it from there.
cURL
$curl = "https://api.bitfinex.com/v1/book/BTCUSD";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $curl);
$ccc = curl_exec($ch);
print_r($ccc);
This would just dump everything.
Or you can parse all the asks and bids into a table using foreach loops. Below is an example with file_get_contents, you could do this with cURL as well of course.
file_get_contents Fiddle example
$fgc = json_decode(file_get_contents("https://api.bitfinex.com/v1/book/BTCUSD"), true);
echo "<table><tr><td>Bids</td><td>Asks</td></tr>";
$bids = $fgc["bids"];
echo "<tr><td valign='top'>";
foreach($bids as $details){
echo "$".$details["price"]." - ".$details["amount"];
echo "<br>";
}
echo "</td><td valign='top'>";
$asks = $fgc["asks"];
foreach($asks as $askDetails){
echo "$".$askDetails["price"]." - ".$askDetails["amount"];
echo "<br>";
}
echo "</td></tr></table>";

Related

Moz api request is being blocked by Incapsula?

When I try to access moz api using below code
$accessID = 'mozscape-key';
$secretKey = 'secert key';
// Set your expires times for several minutes into the future.
// An expires time excessively far in the future will not be honored by the Mozscape API.
$expires = time() + 300;
// Put each parameter on a new line.
$stringToSign = $accessID."\n".$expires;
// Get the "raw" or binary output of the hmac hash.
$binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);
// Base64-encode it and then url-encode that.
$urlSafeSignature = urlencode(base64_encode($binarySignature));
// Specify the URL that you want link metrics for.
$objectURL = "www.seomoz.org";
// Add up all the bit flags you want returned.
// Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
$cols = "103079215108";
// Put it all together and you get your request URL.
// This example uses the Mozscape URL Metrics API.
$requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($objectURL)."?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;
echo $requestUrl;
die;
// Use Curl to send off your request.
$options = array(
CURLOPT_RETURNTRANSFER => true
);
$ch = curl_init($requestUrl);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
$f = fopen('tte.txt','a');
fwrite($f,$content);
fclose($f);
print_r($content);
The out it return is below
<html style="height:100%">
<head>
<meta content="NOINDEX, NOFOLLOW" name="ROBOTS">
<meta content="telephone=no" name="format-detection">
<meta content="initial-scale=1.0" name="viewport">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<title></title>
</head>
<body style="margin:0px;height:100%">
<iframe frameborder="0" height="100%" marginheight="0px" marginwidth="0px"
src="/_Incapsula_Resource?CWUDNSAI=9&xinfo=10-113037580-0%200NNN%20RT(1470041335360%200)%20q(0%20-1%20-1%20-1)%20r(0%20-1)%20B12(8,811001,0)%20U5&incident_id=220010400174850153-812164000562037002&edet=12&cinfo=08000000"
width="100%">Request unsuccessful. Incapsula incident ID:
220010400174850153-812164000562037002</iframe>
<meta content="NOINDEX, NOFOLLOW" name="ROBOTS">
<meta content="telephone=no" name="format-detection">
<meta content="initial-scale=1.0" name="viewport">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<iframe frameborder="0" height="100%" marginheight="0px" marginwidth="0px"
src="/_Incapsula_Resource?CWUDNSAI=9&xinfo=6-31536099-0%200NNN%20RT(1470041496215%200)%20q(0%20-1%20-1%20-1)%20r(0%20-1)%20B12(8,811001,0)%20U5&incident_id=220010400174850153-224923142338658566&edet=12&cinfo=08000000"
width="100%">Request unsuccessful. Incapsula incident ID:
220010400174850153-224923142338658566</iframe>
</body>
</html>
Seems like incapsula is treating request as robot. Can anyone please help me how I can fix it.
If you said you are using the $requestUrl to a GET (in browser) it works fine, try combining your options array.
It should look like this:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $requestUrl,
CURLOPT_USERAGENT => 'Maybe You Need Agent?'
));
Note about agent (taken from web):
cURL is a behemoth, and has many many possibilities. Some sites might
only serve pages to some user agents, and when working with APIs, some
might request you send a specfici user agent, this is something to be
aware of.
Also worth checking - you have ID of failture from Incapsula - 220010400174850153-224923142338658566
Can you check the logs and see what is there?

I cant save some facebook images to my server as it does not understand the file

I am using the facebook graph api and it was working well until I realised that some of the jpg files have a query string at the end that is making them unusable.
e.g.
https://scontent.xx.fbcdn.net/hphotos-xaf1/v/t1.0-9/487872_451835128174833_1613257199_n.jpg?oh=621bed79f5436e81c3e219c86db8f0d9&oe=560F3D0D
I have tried stripping off everything after .jpg in the hope that it would still load the image but unfortunately it doesnt.
In the following code take the $facebook_image_url to be the one above. This works fine when the url ends in .jpg but fails on the above. As a note, I am converting the name to a random number
$File_Name = $facebook_image_url;
$File_Ext = '.jpg';
$Random_Number = rand(0, 9999999999); //Random number to be added to name.
$NewFileName = $Random_Number.$File_Ext; //new file name
$local_file = $UploadDirectory.$NewFileName;
$remote_file = $File_Name;
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
$image = new Imagick(DIR_TEMPORARY_IMAGES.$NewFileName);
The error Im getting is
Fatal error: Uncaught exception 'ImagickException' with message 'Not a
JPEG file: starts with 0x3c 0x21 `/mysite/temp-images/1849974705.jpg'
# jpeg.c/EmitMessage/232'
I can confirm the image isnt saving as a proper jpg, just a small 3KB file with the name 1849974705.jpg (or other random numbers)
Is there either
A: A way of getting those images from facebook as raw jpg
or
B: A way of converting them succesfully to jpgs
You could always download the image using file_get_contents()
This code works for me...
file_put_contents("image.jpg", file_get_contents("https://scontent.xx.fbcdn.net/hphotos-xaf1/v/t1.0-9/522827_10152235166655545_26514444_n.jpg?oh=1d52a86082c7904da8f12920e28d3687&oe=5659D5BB"));
Just because something has .jpg in the URI doesn't mean it's an image.
Getting that URL via wget gives the result:
<!DOCTYPE html>
<html lang="en" id="facebook">
<head>
<title>Facebook | Error</title>
<meta charset="utf-8">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="cache-control" content="no-store">
<meta http-equiv="cache-control" content="max-age=0">
<meta http-equiv="expires" content="-1">
<meta http-equiv="pragma" content="no-cache">
<meta name="robots" content="noindex,nofollow">
<style>
....
....
i.e. it's not an image, exactly as the error message is telling you.

PHP Google Contact API generateing HTTP/1.0 401 Authorization required

I am using Google Contact API V3 with OAuth 2.0 to retrieve the contacts from Google Account, I am getting a OAuth Token from a child window using jQuery, and after post that token to another file which should get the User's Contacts, but when I pass the token to get the contacts from Google, but it gives an error "Warning: file_get_contents(https://www.google.com/m8/feeds/contacts/default/full&oauth_token=[token]) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 401 Authorization required in ...\getContacts.php on line 7"
Here is my code for reference:
In my index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery-1.8.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var windowSizeArray = [ "width=200,height=200",
"width=300,height=400,scrollbars=yes" ];
$(document).ready(function(){
$('#newWindow').click(function (event){
var url = $(this).attr("href");
var windowName = "popUp";//$(this).attr("name");
var windowSize = windowSizeArray[ $(this).attr("rel") ];
window.open(url, windowName, windowSize);
event.preventDefault();
});
});
function getContacts(accessToken){
$.post("getContacts.php?token="+accessToken,function(data){
$("#contacts").html(data);
});
}
</script>
</head>
<body>
Click Here! to import your contact.
<div id="contacts"></div>
in my childWindow.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>
</head>
<body>
<?php
$authcode= $_GET["code"];
$clientid='My Client ID';
$clientsecret='My Client Secret';
$redirecturi='http://localhost/googleContacts/validate.php';
$fields=array(
'code'=> urlencode($authcode),
'client_id'=> urlencode($clientid),
'client_secret'=> urlencode($clientsecret),
'redirect_uri'=> urlencode($redirecturi),
'grant_type'=> urlencode('authorization_code')
);
//url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string=rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//to trust any ssl certificates
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//extracting access_token from response string
$response= json_decode($result);
$accesstoken= $response->access_token;
//echo "<span>".$accesstoken."</span><hr>";
?>
<script type="text/javascript">
$(document).ready(function() {
window.opener.getContacts("<?php echo $accesstoken; ?>");
window.close();
});
</script>
And finally in my getContacts.php:
<?php
$accesstoken = $_REQUEST['token'];
//passing accesstoken to obtain contact details
$xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/default/full&oauth_token='.$accesstoken);
//reading xml using SimpleXML
$xml= new SimpleXMLElement($xmlresponse);
foreach($xml->entry as $content){
$nameEmail = "";
if(!empty($content->title)){
$nameEmail .= "<span style=\"text-decoration:underline;\">".$content->title."</span>: ";
}
$gd = $content->children('http://schemas.google.com/g/2005');
if($gd){
$nameEmail .= $gd->attributes()->address."<hr>";
echo $nameEmail;
}else{
echo $nameEmail."<hr>";
}
}
?>
Please tell me where is the mistake. Thanks in advance
To allow https for file_get_contents() you should have enabled the php extension php_openssl.dll.
Make sure that in your php.ini you have this lines:
extension=php_openssl.dll
allow_url_fopen = On

PHP with freebase /location/contains

I have this php file that gets the points (id,name,geolocation) of london.The problem is that I get the correct results as a json format but when i decode it and trying to get to contains array of results I get an error.How i can get the data from '/location/location/contains' attribute?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>search</title>
</head>
<body>
<?php
function freebasequery ($fid){
$query = array(array('id' => $fid, '/location/location/contains'=>array(array('id'=>NULL,'name' => NULL,'/location/location/geolocation' =>array(array('/location/geocode/longitude' =>NULL,'/location/geocode/latitude' => NULL))))));
$query_envelope = array('query' => $query);
$service_url = 'http://api.freebase.com/api/service/mqlread';
$url = $service_url . '?query=' . urlencode(json_encode($query_envelope));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$points=freebasequery('/en/london');
//echo $points;
$results=json_decode($points)->result;
foreach($results as $poi){
echo $poi->id;
$contains="/location/location/contains";
$poisarray=$poi->$contains;
foreach($poisarray as $point){
echo $point->id;
}
}
?>
</body>
</html>
The error it was on json_decode ( It requires to have a true) the solution is:
json_decode($points,true);
and then I can access the data array I want like this:
$results["result"][0]["/location/location/contains"];

Facebook API : Cannot register an achievement with facebook

I spent a lot of time trying to register an achievement with facebook using grahp API, but
I am always getting this :
{"error":{"type":"OAuthException","message":"(#2) Object at achievement URL is not of type game.achievement"}}
The code is :
$achievementUrl = 'http://www.dappergames.com/test.html';
$url = 'https://graph.facebook.com/' . $appID . '/achievements?achievement=' .
$achievementUrl . '&display_order=' . $order . '&access_token=' . $accessToken . '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$data = curl_exec($ch);
print_r($data);
Any help will be greatly appreciated.
Thanks
Looking at your test, you're missing the most important tag, og:url.
Facebook uses that tag to find the location of where to parse the information, I was having a similar problem on adding achievements to my app until I figured that out. Here's how mine looks. Mine passes the debugger and works currently on Facebook.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Game Loader</title>
<meta property="og:type" content="game.achievement"/>
<meta property="og:title" content="Game Loader"/>
<meta property="og:url" content="FULL LINK TO THIS PAGE"/>
<meta property="og:description" content="You loaded the gam!e"/>
<meta property="og:image" content="Link to 50x50 image"/>
<meta property="og:points" content="10"/>
<meta property="fb:app_id" content="YOUR_APP_ID"/>
</head>
<body>
</body>
</html>
After updating your HTML to this, run it through the debugger linked above so that it is updated on Facebook's end (and to see if you receive any warnings) and then create it and give it to users.
Here's how I created it:
curl -D "achievement=[URL TO ACHIEVEMENT]&access_token=[APPLICATION ACCESS TOKEN]" https://graph.facebook.com/APP_ID/achievements
and I then rewarded it to users using the Facebook Javascript API on my canvas application like so:
FB.api('/'+user_id+'/achievements', 'POST', { 'access_token': [APPLICATION ACCESS TOKEN], 'achievement':[FULL URL]},
function(response) {
if(console)
console.log(response);
});
Make sure that you have the correct Open Graph meta tags in your achievement URL $achievementUrl:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#">
<head>
<title>ACHIEVEMENT_TITLE</title>
<meta property="og:type" content="game.achievement"/>
<meta property="og:url" content="URL_FOR_THIS_PAGE"/>
<meta property="og:title" content="ACHIEVEMENT_TITLE"/>
<meta property="og:description" content="ACHIEVEMENT_DESCRIPTON"/>
<meta property="og:image" content="URL_FOR_ACHIEVEMENT_IMAGE"/>
<meta property="og:points" content="POINTS_FOR_ACHIEVEMENT"/>
<meta property="fb:app_id" content="YOUR_APP_ID"/>
</head>
<body>
Promotional content for the Achievement.
This is the landing page where a user will be directed after
clicking on the achievement story.
</body>
</html>
The above is just an example from this post. Note the first meta tag in the example? I suppose it's missing in your page.
<meta property="og:title" content="Game Loader"/>
<meta property="og:url" content="FULL LINK TO THIS PAGE"/>
These are required fields. Provide all the required fields and try again with the debugger tool, if debugger tool passes your page then you are ok to register achievement.

Categories