Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: CA Coins on July 14, 2012, 11:01:25 PM



Title: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 14, 2012, 11:01:25 PM
Does anybody have working code for connecting to the CampBX API with PHP?  I can get market data and account data if I post the auth info through a form, but I can't seem to connect to account data with cURL or file_get_contents in PHP.  The linked PHP library by Brandon Beasley also doesn't work for me for some reason.  Can't get a response from their support either for the last 2 weeks.  If anybody has working code to get account info via cURL or file_get_contents in PHP, I would much appreciate it.  Thanks.

EDIT:  Added 5 BTC bounty for whoever helps me solve this problem.  Thanks.


Title: Re: Acessing CampBX API with PHP
Post by: BCB on July 14, 2012, 11:12:01 PM
Have you requested api credentials access?  You have the request access.  There is no on and off in the user interface.  Then you can use your existing credentials.


Title: Re: Acessing CampBX API with PHP
Post by: CA Coins on July 14, 2012, 11:22:13 PM
Yeah, I have been granted API access.  Interestingly, if I post the credentials through a form, then I can access myfunds, myorders, etc.  But if I try to post the same data with cURL, I get
"Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request."

my cURL parameters:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://CampBX.com/api/myfunds.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=myUser&pass=myPassword');
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
print_r($res);
curl_close($ch);

if I use file_get_contents, I just get a blank page.


Title: Re: Acessing CampBX API with PHP
Post by: BCB on July 14, 2012, 11:34:14 PM
Yeah, I have been granted API access.  Interestingly, if I post the credentials through a form, then I can access myfunds, myorders, etc.  But if I try to post the same data with cURL, I get
"Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request."

my cURL parameters:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://CampBX.com/api/myfunds.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=myUser&pass=myPassword');
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
print_r($res);
curl_close($ch);

if I use file_get_contents, I just get a blank page.



Try:

 $params['user'] = "username";
 $params['pass'] = "password";

 $postData = http_build_query($params, '', '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://CampBX.com/api/myfunds.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
print_r($res);
curl_close($ch);


Title: Re: Acessing CampBX API with PHP
Post by: CA Coins on July 15, 2012, 12:42:01 AM
Yeah, I have been granted API access.  Interestingly, if I post the credentials through a form, then I can access myfunds, myorders, etc.  But if I try to post the same data with cURL, I get
"Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request."

my cURL parameters:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://CampBX.com/api/myfunds.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=myUser&pass=myPassword');
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
print_r($res);
curl_close($ch);

if I use file_get_contents, I just get a blank page.



Try:

 $params['user'] = "username";
 $params['pass'] = "password";

 $postData = http_build_query($params, '', '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://CampBX.com/api/myfunds.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
print_r($res);
curl_close($ch);

Thanks for your help, but I got the same response.  "Bad request..."


Title: Re: Acessing CampBX API with PHP
Post by: BCB on July 15, 2012, 01:02:28 AM
Any output in your apache error_log?


Title: Re: Acessing CampBX API with PHP
Post by: CA Coins on July 15, 2012, 01:14:06 AM
I am actually running this on win server 2008.  I don't see anything in the PHP error log or the IIS error log.  I can access the other exchanges (mtgox, bitfloor, intersango) with no issues.  Weird it is just with CampBX.


Title: Re: Acessing CampBX API with PHP
Post by: CA Coins on July 19, 2012, 03:57:14 AM
I am surprised nobody has working code for accessing CampBX API with PHP.  Maybe it is just my setup?  Either way, I am going to throw in a 5 BTC bounty to whoever helps me solve this problem.  Please post or PM me.  Thanks.


Title: Re: Acessing CampBX API with PHP
Post by: gweedo on July 19, 2012, 04:00:19 AM
https://bitbucket.org/brandonbeasley/campbx-php/overview
the have a library

also you may need to do basic auth to use curl


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 19, 2012, 04:01:34 AM
Thanks for the link, but his library doesn't work for me for some reason.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: gweedo on July 19, 2012, 04:03:24 AM
Code:
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTChash; '.php_uname('s').'; PHP/'.phpversion().')');

add this to your code, i looked at his source and this how he gets it to work


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: BCB on July 19, 2012, 02:15:56 PM
CA Coins,

The code i posted works fine with my credentials on a CentOS server.  I think it an MS Server issue.  Have you contacted Keyur directly.  He's been very helpful with my requests. When you do find a solution please post.

Thanks.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: unfinishe on July 19, 2012, 04:58:06 PM
I'm no expert, but here are my suggestions:

First, make sure that you have OpenSSL installed. http://www.webhostingtalk.com/showthread.php?t=929949

Second, check to see if there are any proxy settings that aren't getting carried through. http://forums.iis.net/p/1173290/1973896.aspx#1973896

Third, try implementing the headers and curl options from the last post here: http://bytes.com/topic/php/answers/734722-getting-php-login-somewhere-post-username-password-then-get-cookies



Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 20, 2012, 04:16:43 AM
Thanks for the replies.  I will do some testing in the next couple of days and report back the results.  Thanks.

EDIT:  Still no go.  I verified that I am running php 5.3.9, curl 7.21.7 with openssl 0.9.8r.  I tried adding the cert and it didn't solve the problem.  I also tried running BCB's code on Ubuntu 11.10, Apache 2.0, PHP 5.3.6, curl 7.21.6, openssl 1.0.0e.  I think something is not set correctly and the CampBX server is still rejecting my requests with the bad request error.  I don't see any errors in the Apache error log either.  I am going to try contacting CampBX again to see if they can find anything in their logs.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: drakahn on July 25, 2012, 10:58:57 AM
What about

$params['user'] = "username";
$params['pass'] = "password";

$postData = http_build_query($params, '', '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://CampBX.com/api/myfunds.php");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");


$res = curl_exec($ch);
print_r($res);
curl_close($ch);

?


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 28, 2012, 07:55:50 PM
Thanks for the suggestion, but still getting a 404 bad request error with the code below.

Code:
$params['user'] = "myUser";
$params['pass'] = "myPass";

$postData = http_build_query($params, '', '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://CampBX.com/api/myfunds.php");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
//with or without:  curl_setopt($ch, CURLOPT_CAINFO, "path/cacert.crt");

$res = curl_exec($ch);
print_r($res);
curl_close($ch);


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: scribe on July 28, 2012, 08:48:55 PM
Does curl_error() give you anything? http://php.net/manual/en/function.curl-error.php

Looking at the curl_setopt() page has some things you could try (http://php.net/manual/en/function.curl-setopt.php):

Try using basic auth with:

Code:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );


Try using different SSL versions:

Code:
curl_setopt($curl, CURLOPT_SSLVERSION,3); // or try setting to 2

The manual page says CURLOPT_SSL_VERIFYHOST can be either 1 or 2 with 2 being the default, so try 1 (instead of 0):

Code:
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);

Otherwise maybe it's something to do with encodings being used - see the CURLOPT_ENCODING option. If there's a way to show the headers you're sending, you could try comparing what you're sending across different machines, and if one works, what difference in headers there are.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: BCB on July 28, 2012, 11:01:16 PM
Code:
curl_setopt($curl,CURLOPT_POST, true);

you forgot this so it will send as a post

Gweedo,

That is redundant when you are already calling

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

Try it.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: paraipan on July 28, 2012, 11:19:05 PM
Watching thread, and the skills some of you show off in here.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: unclemantis on July 29, 2012, 01:03:23 AM
Code:
sudo apt-get install ruby1.9.1

Solved!

19nUeM1qJmkpr8KYf9frtfWj6MCX7jpjb


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 29, 2012, 08:12:38 AM
Does curl_error() give you anything? http://php.net/manual/en/function.curl-error.php

Nope, no errors.

Quote

Try using different SSL versions:

Code:
curl_setopt($curl, CURLOPT_SSLVERSION,3); // or try setting to 2


Bingo!  That did the trick.  

If verifyhost set as 1, then also need the line to point to the certificate, but here is a working code.

Code:
$params['user'] = "myUser";
$params['pass'] = "myPass";
$postData = http_build_query($params, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://CampBX.com/api/myfunds.php");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_CAINFO, "path to certificate.crt");
//curl_setopt($ch,CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($ch, CURLOPT_SSLVERSION,3); // or try setting to 2  //This did the trick

$res = curl_exec($ch);
print_r($res);
echo 'Curl error: ' . curl_error($ch);
curl_close($ch);


Thanks to everybody for your help.  scribe, can you please send or post your address for the 5BTC bounty?  Also, to BCB, gweedo, unfinishe, and drakahn, please send or post your address so I can send you a small donation for helping out.  


Title: Re: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: drakahn on July 29, 2012, 08:24:15 AM
1MEYHC9c5gmJ4TPWCoFWW9Ey1TGLUjfMKi

Glad you got it worked out


Title: Re: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: BCB on July 29, 2012, 01:01:20 PM
Ahhhh.  MS Server requires the Certificate. Linux does not.  Good to know. It makes me think that Campbx is also hosted on an MS Server box as you said you could authenticate to the other exchanges but not to Campbx.  Glad you got it working!


Title: Re: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: unclemantis on July 29, 2012, 02:03:44 PM
PHP on IIS? UGH! No wonder!

DOOD! Switch to Linux for ANYTHING OPEN SOURCE!


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: scribe on July 29, 2012, 03:06:06 PM
Try using different SSL versions:

Code:
curl_setopt($curl, CURLOPT_SSLVERSION,3); // or try setting to 2


Bingo!  That did the trick.  


Thanks to everybody for your help.  scribe, can you please send or post your address for the 5BTC bounty?  Also, to BCB, gweedo, unfinishe, and drakahn, please send or post your address so I can send you a small donation for helping out.  

Awesome - my 'tweak stuff til it does something different' approach works again... PM'd.

Now just got to figure out why php on osx hates anything https.


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: unclemantis on July 29, 2012, 03:57:55 PM
Try using different SSL versions:

Code:
curl_setopt($curl, CURLOPT_SSLVERSION,3); // or try setting to 2


Bingo!  That did the trick.  


Thanks to everybody for your help.  scribe, can you please send or post your address for the 5BTC bounty?  Also, to BCB, gweedo, unfinishe, and drakahn, please send or post your address so I can send you a small donation for helping out.  

Awesome - my 'tweak stuff til it does something different' approach works again... PM'd.

Now just got to figure out why php on osx hates anything https.

Saint Jobs needs to spy on you. He can't spy on your through https :P


Title: Re: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 29, 2012, 07:24:50 PM
Yeah, I agree.  Linux is a good system and much better for running PHP.  I have a few thousand lines of code in classic asp so the migration has been happening slowly but surely. 

Thanks again for everybody's help.  The bounty and most of the donations have been sent.  BCB, I got a 2BTC donation coming your way if you're interested (I don't see an address in your sig).


Title: Re: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: BCB on July 29, 2012, 07:44:57 PM
Yeah, I agree.  Linux is a good system and much better for running PHP.  I have a few thousand lines of code in classic asp so the migration has been happening slowly but surely. 

Thanks again for everybody's help.  The bounty and most of the donations have been sent.  BCB, I got a 2BTC donation coming your way if you're interested (I don't see an address in your sig).

I appreciate that but I didn't solve the problem.  Happy to help you out.

Put the 2BTC towards a new linux box! ;)

Thanks!


Title: Re: [CLOSED] Bounty 5 BTC - Acessing CampBX API with PHP
Post by: CA Coins on July 29, 2012, 08:14:14 PM
Cool dude.  Thanks for your help!


Title: Re: Bounty 5 BTC - Acessing CampBX API with PHP
Post by: scribe on July 29, 2012, 08:48:44 PM
Thanks for the donation, CA - I'll keep it away from satoshidice I think :) I do a fair bit of dev work on Windows Server and then deploy to a Linux box, so I'll keep an eye out for this one in future...

Now just got to figure out why php on osx hates anything https.

Saint Jobs needs to spy on you. He can't spy on your through https :P

Pretty sure the big bearded dude in the sky could pull a MITM attack if he wanted to. Does God have root?