PHP authentication to Coinbase Exchange private API - php

I'm trying to get authenticated to the Coinbase Exchange private API to get balances and place/cancel orders. Below is a demo version of the code that I'm trying to use. I have followed the docs but I keep getting the following error:
{"message":"invalid signature"}
Can someone please tell me what I'm doing wrong? :)
EDIT: I have modified the below code based on Sašo's answer so it now works.
<?php
$key = "";
$secret = "";
$passphrase = "";
$time = time();
$url = "https://api.gdax.com/accounts";
$data = $time."GET"."/accounts";
echo $data . "<br/>";
$sign = base64_encode(hash_hmac("sha256", $data, base64_decode($secret), true));
echo $sign . "<br/>";
$headers = array(
'CB-ACCESS-KEY: '.$key,
'CB-ACCESS-SIGN: '.$sign,
'CB-ACCESS-TIMESTAMP: '.$time,
'CB-ACCESS-PASSPHRASE: '.$passphrase,
'Content-Type: application/json'
);
var_dump($headers);
echo $url;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
echo $res;
}

You are missing a true parameter for raw_output when doing a hash_hmac
http://php.net/manual/en/function.hash-hmac.php
raw_output: When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.

Could be something silly. double check your key, pass phrase and secret. Also check the permissions you enabled when creating the key.

Sašo Matejina is right. you should probably better user server user agent too.
$userAgent=$_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

Related

The requested URL returned error: 400 Bad Request

I have been working on setting up the frase api. and created the following curl snippet.
<?php
$url = 'http://api.frase.io/api/v1/process_url';
//The data you want to send via POST
$fields = ['url' => 'https://firstsiteguide.com/best-gaming-blogs/', 'token' => "dd528796a9924dae9962bc5bd7ccdb20"];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($fields));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
echo "<br/>CURL ERROR: ". $error_msg ."<br/>";
}else{
print "curl response is:" . $response ;
}
curl_close ($ch);
?>
I am not sure why, But I am receiving the following error for the same
The requested URL returned error: 400 Bad Request
Can help me identify what part of code I am missing or doing wrong. Thank you so much in advance.
You're passing the token as a body parameter instead of a header. The body parameter needs to be sent as a JSON encoded string as mentioned on the API documentation page. Also, you need to remove or at least increase the cURL timeout value as it takes time to fetch, process and return a value from the API end. Note that the API will return the response in JSON format.
So, the complete code should be as:
<?php
$url = 'http://api.frase.io/api/v1/process_url'; //The endpoint url you want to send data via POST
$headers = ['token: dd528796a9924dae9962bc5bd7ccdb20']; // add this line, headers
$fields = ['url' => 'https://firstsiteguide.com/best-gaming-blogs/']; // modify this line, body
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // add this line
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); // modify this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
$response = curl_exec($ch);
if (curl_errno($ch))
{
$error_msg = curl_error($ch);
echo "<br/>CURL ERROR: " . $error_msg . "<br/>";
}
else
{
print($response);
// $values = json_decode($response, true); // this is an array with all the values
}
curl_close($ch);
?>

How to use mobile detect api (useragentinfo.co)

i am getting too much information from api, i can't use this, I want only "device_model" and "device_type" from this result, and how i can do it?
What i am getting this result:
{"device_model":"Emulator","os_version":"7","browser":"Chrome","browser_version":"60","os":"Windows","device_type":"Desktop","useragent":"Mozilla/5.0
(Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/61.0.3163.100
Safari/537.36","device_brand":"Unknown","is_bot":false}
Mobile detect api
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
// get api token at https://useragentinfo.co/
$token = "#API_TOKEN";
$url = "https://useragentinfo.co/api/v1/device/";
$data = array('useragent' => $useragent);
$headers = array();
$headers[] = "Content-type: application/json";
$headers[] = "Authorization: Token " . $token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status != 200 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
echo $json_response;
?>
If you are getting a standard json response from the api, you can just json_decode() it and use it like this:
<?php
// Do your curl request and get json_response here...
// Decode json response
$result = json_decode($json_response);
// All of the fields are then accessed like this:
// $result->device_model (for e.g.)
?>
<b>Device Model</b>: <?php echo $result->device_model ?>
<br>
<b>Device Type</b>: <?php echo $result->device_type ?>
etc...
If you prefer to work with array instead, you can do this:
// Decode json response
$result = json_decode($json_responsem true);
// Then use it like this:
// $result['device_model'] etc...

Scrape website with javascript using cURL

I try to scrape data of this website:
http://ntthnue.edu.vn/tracuudiem
First, when I insert the SBD field with data 'TS4740', I can successfully get the result. However, when I try to run this code:
Here is my PHP cURL code:
<?php
function getData($id) {
$url = 'http://ntthnue.edu.vn/tracuudiem';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['sbd' => $id]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo getData('TS4740');
I just got the old page. Can anybody explain why? Thank you!
Make sure you add all the necessary headers and input data. The server that is processing this request can do all kinds of checks to see if it's a "valid" form request. As such you need to spoof the request to be as close to a regular browser request as possible.
Use tools like Chrome Dev Tools to see both the request and respons headers that are sent between the server and your browser to better understand what you curl setup should be like. And further use a app like Postman to make the request simulation super easy and to see what works and not.
Working example:
<?php
function getData($id) {
$url = 'http://ntthnue.edu.vn/tracuudiem';
$ch = curl_init($url);
$postdata = 'namhoc=2015-2016&kythi_name=Tuy%E1%BB%83n+sinh+v%C3%A0o+l%E1%BB%9Bp+10&hoten=&sbd='.$id.'&btnSearch=T%C3%ACm+ki%E1%BA%BFm';
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin: http://ntthnue.edu.vn',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'Referer: http://ntthnue.edu.vn/tracuudiem',
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo getData('TS4740');

Google voice PHP OAuth 2.0

I once used a PHP Class Library to connect to Google Voice to send SMS Text Messages. The call would work something like this:
$gv = new GoogleVoice("GmailAccount", "GmailPassword");
$gv->sms("PhoneNumber", "TextMsg");
It worked flawlessly until recently, as of 4/20/2015, Google stopped supporting old methods of logging in to Google account. So my script stopped working giving 500 error. Google says you have to use OAuth 2.0 to authenticate however I haven't found any examples online on how to accomplish this with Google Voice. The code is below, I have not written this, please let me know how to adjust the code to use Google's OAuth System.
/*
Version 0.2
License This code is released under the MIT Open Source License. Feel free to do whatever you want with it.
Author lostleon#gmail.com, http://www.lostleon.com/
LastUpdate 05/28/2010
Usage:
*/
class GoogleVoice
{
public $username;
public $password;
public $status;
private $lastURL;
private $login_auth;
private $inboxURL = 'https://www.google.com/voice/m/';
private $loginURL = 'https://www.google.com/accounts/ClientLogin';
private $smsURL = 'https://www.google.com/voice/m/sendsms';
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
}
public function getLoginAuth()
{
$login_param = "accountType=GOOGLE&Email={$this->username}&Passwd={$this->password}&service=grandcentral&source=com.lostleon.GoogleVoiceTool";
$ch = curl_init($this->loginURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20");
curl_setopt($ch, CURLOPT_REFERER, $this->lastURL);
curl_setopt($ch, CURLOPT_POST, "application/x-www-form-urlencoded");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_param);
$html = curl_exec($ch);
$this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
$this->login_auth = $this->match('/Auth=([A-z0-9_-]+)/', $html, 1);
return $this->login_auth;
}
public function get_rnr_se()
{
$this->getLoginAuth();
$ch = curl_init($this->inboxURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array("Authorization: GoogleLogin auth=".$this->login_auth, 'User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$html = curl_exec($ch);
$this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
$_rnr_se = $this->match('!<input.*?name="_rnr_se".*?value="(.*?)"!ms', $html, 1);
return $_rnr_se;
}
public function sms($to_phonenumber, $smstxt)
{
$_rnr_se = $this->get_rnr_se();
$sms_param = "id=&c=&number=".urlencode($to_phonenumber)."&smstext=".urlencode($smstxt)."&_rnr_se=".urlencode($_rnr_se);
$ch = curl_init($this->smsURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array("Authorization: GoogleLogin auth=".$this->login_auth, 'User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, $this->lastURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sms_param);
$this->status = curl_exec($ch);
$this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $this->status;
}
private function match($regex, $str, $out_ary = 0)
{
return preg_match($regex, $str, $match) == 1 ? $match[$out_ary] : false;
}
}
I will send you to the next answer:
https://stackoverflow.com/a/4131915/2992810
unfortunate google voice had changed their API so you can not use it anymore.
https://github.com/aaronpk/Google-Voice-PHP-API (look at the comments in the head)
Google Voice is not an open API, so they are no maintaining it.
Sorry to tell you but in my own experience today SMS services are so cheap that it will cost you less to actually buy service license than fight with Google and their constant API changes, your website can always go down because of such change.
Thinks about your time as a resource, spending your time will cost you more!
GV4J is a Java library that is able to login to Google Voice, so it might be a good reference for updating your PHP code to be able to authenticate.

Getting Data from Bitsnoop API

I am using the following piece of code to get tracker data (converted from JSON to PHP) and find the sum total of the number of seeders from the BitSnoop API:
$hash = "98C5C361D0BE5F2A07EA8FA5052E5AA48097E7F6";
if(!function_exists("curl_init")) die("cURL extension is not installed");
$url = "http://bitsnoop.com/api/trackers.php?hash=" . $hash . "&json=1";
echo $url;
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$myarr = json_decode($r,true);
print_r($myarr);
But the script is not able to retrieve ANY data from the URL.
Chrome's view-source is working on the page, but any other way of retrieving the source of the page, either via viewsource.in or i-tools don't seem to retrieve any data from the URL as well.
Could anyone explain why is it so?
And please provide an alternative way to accomplish the retrieval.
Thanks in advance !
You should pretend to be a legit browser:
$hash = "98C5C361D0BE5F2A07EA8FA5052E5AA48097E7F6";
if(!function_exists("curl_init")) die("cURL extension is not installed");
$url = "http://bitsnoop.com/api/trackers.php?hash=" . $hash . "&json=1";
$headers = array(
'Host: bitsnoop.com',
'Connection: keep-alive',
'Cache-Control: max-age=0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36',
'Accept-Encoding: deflate,sdch',
'Accept-Language: ru,en-US;q=0.8,en;q=0.6');
echo $url;
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$r=curl_exec($ch);
curl_close($ch);
$myarr = json_decode($r,true);
print_r($myarr);
And also it's a good idea to test curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); , however, I did not look if they use or not HTTP redirect

Categories