Bitcoin Forum
May 06, 2024, 05:32:11 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: JSON-RPC Client PHP converted to cURL instead of fopen  (Read 4971 times)
gweedo (OP)
Legendary
*
Offline Offline

Activity: 1498
Merit: 1000


View Profile
August 03, 2012, 11:44:35 PM
 #1

fopen is insecure and not correct when trying to communicating with remote sites. 
I just rewrote the http post part of the code so just in case anyone was wanted it

http://pastebin.com/vREuHVr5
Activity + Trust + Earned Merit == The Most Recognized Users on Bitcointalk
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714973531
Hero Member
*
Offline Offline

Posts: 1714973531

View Profile Personal Message (Offline)

Ignore
1714973531
Reply with quote  #2

1714973531
Report to moderator
1714973531
Hero Member
*
Offline Offline

Posts: 1714973531

View Profile Personal Message (Offline)

Ignore
1714973531
Reply with quote  #2

1714973531
Report to moderator
1714973531
Hero Member
*
Offline Offline

Posts: 1714973531

View Profile Personal Message (Offline)

Ignore
1714973531
Reply with quote  #2

1714973531
Report to moderator
onpdm
Newbie
*
Offline Offline

Activity: 26
Merit: 0


View Profile
February 21, 2013, 02:52:31 PM
 #2

Don't know why nobody did a reply to your posting, but I just want to say thank you. Wink

I've moved my script to a new server and the connection to bitcoind did not work anymore ... I tried over 1 hour with this shit, after I detected that fopen is disabled by php.ini security configuration. ;( jsonRPC always said only "Unable to connect" in fact of thrown PHP exception, so I did not find out that fopen is the problem. After some help of my friend Google I found your script!

Thank you, great! Wink
gweedo (OP)
Legendary
*
Offline Offline

Activity: 1498
Merit: 1000


View Profile
February 21, 2013, 06:54:13 PM
 #3

No problem, enjoy!
r3wt
Hero Member
*****
Offline Offline

Activity: 686
Merit: 504


always the student, never the master.


View Profile
January 13, 2014, 02:05:36 AM
 #4

+1 for this post Gweedo. helped me eliminate the vulnerability of having
Code:
allow_url_fopen : On
in php.ini

My negative trust rating is reflective of a personal vendetta by someone on default trust.
r3wt
Hero Member
*****
Offline Offline

Activity: 686
Merit: 504


always the student, never the master.


View Profile
January 13, 2014, 02:21:57 AM
 #5

+1 for this post Gweedo. helped me eliminate the vulnerability of having
Code:
allow_url_fopen : On
in php.ini

That is why I am here.

Quote
Status: 0/unconfirmed, has not been successfully broadcast yet
Date: 1/12/2014 20:21
To: 1GweedoZJYb5CNLfSaBgBBYS2y7BMVb2Wo
Debit: -0.01 BTC
Transaction fee: -0.0001 BTC
Net amount: -0.0101 BTC
Transaction ID: 148968123e5ceb0c4f1c80f39e48b4d4e659c62464abbea70766a26d5d95ed8e



My negative trust rating is reflective of a personal vendetta by someone on default trust.
r3wt
Hero Member
*****
Offline Offline

Activity: 686
Merit: 504


always the student, never the master.


View Profile
January 13, 2014, 05:43:25 AM
 #6

+1 for this post Gweedo. helped me eliminate the vulnerability of having
Code:
allow_url_fopen : On
in php.ini

That is why I am here.

Quote
Status: 0/unconfirmed, has not been successfully broadcast yet
Date: 1/12/2014 20:21
To: 1GweedoZJYb5CNLfSaBgBBYS2y7BMVb2Wo
Debit: -0.01 BTC
Transaction fee: -0.0001 BTC
Net amount: -0.0101 BTC
Transaction ID: 148968123e5ceb0c4f1c80f39e48b4d4e659c62464abbea70766a26d5d95ed8e



Thank you sir

Thank you as well. If you need a job doing some PDO, you know who to call...

My negative trust rating is reflective of a personal vendetta by someone on default trust.
Crypto-Maniac
Newbie
*
Offline Offline

Activity: 57
Merit: 0


View Profile WWW
May 17, 2014, 08:22:04 AM
 #7

Hello ,
i have tried the script but i couldn't make it work on my install , i have curl enable
it work well with the version fopen but would love to convert to curl for security reason
if someone can update this one

thx you
gweedo (OP)
Legendary
*
Offline Offline

Activity: 1498
Merit: 1000


View Profile
May 17, 2014, 08:31:43 AM
 #8

Hello ,
i have tried the script but i couldn't make it work on my install , i have curl enable
it work well with the version fopen but would love to convert to curl for security reason
if someone can update this one

thx you

I tested it on php 5.4 and it worked. Can I see your code?
Crypto-Maniac
Newbie
*
Offline Offline

Activity: 57
Merit: 0


View Profile WWW
May 17, 2014, 09:37:52 AM
 #9

thx gweedo

here is my original file

http://pastebin.com/w6khV4Gp

& i took your code and insert it at HTTP POST
Code:
// performs the HTTP POST
                $ch = curl_init($this->url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
                $response = json_decode(curl_exec($ch),true);
                curl_close($ch);
http://pastebin.com/R0uc8xKb
Crypto-Maniac
Newbie
*
Offline Offline

Activity: 57
Merit: 0


View Profile WWW
May 17, 2014, 09:57:55 AM
 #10

im on php 5.5 , i cant downgrade since my script is developed under php 5.5.4

thx you
Catastrough
Newbie
*
Offline Offline

Activity: 62
Merit: 0


View Profile
May 17, 2014, 01:46:08 PM
 #11

I need a good example of PHP JSON and Ext controls. I return a JSON formatted dataset and then pass it to an Ext control, but the data does not get rendered. WTF.
Crypto-Maniac
Newbie
*
Offline Offline

Activity: 57
Merit: 0


View Profile WWW
June 03, 2014, 02:13:45 PM
 #12

up please Smiley
r3wt
Hero Member
*****
Offline Offline

Activity: 686
Merit: 504


always the student, never the master.


View Profile
June 03, 2014, 03:27:22 PM
 #13

Here's the original modified ever so slightly to remove errors. if you want it to throw errors so you can use try catch, you'll have to do some searching for gweedo's original version.

Code:
class jsonRPCClient {
private $debug;
private $url;
private $id;
private $notification = false;
public function __construct($url,$debug = false) {
$this->url = $url;
empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
empty($debug) ? $this->debug = false : $this->debug = true;
$this->id = 1;
}
public function setRPCNotification($notification) {
empty($notification) ? $this->notification = false : $this->notification = true;
}
public function __call($method,$params) {
if (!is_scalar($method)) { throw new Exception('Method name has no scalar value'); }              
if (is_array($params)) { $params = array_values($params);}else{ throw new Exception('Params must be given as array'); }
if ($this->notification) {$currentId = NULL; }else{ $currentId = $this->id;}
$request = array( 'method' => $method, 'params' => $params, 'id' => $currentId );
$request = json_encode($request);
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
$ch = curl_init($this->url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = json_decode(curl_exec($ch),true);
curl_close($ch);
if ($this->debug) { echo nl2br($debug); }
if (!$this->notification) {
if ($response['id'] != $currentId) { return $response; }
if (!is_null($response['error'])) { return $response; }
return $response['result'];
}else{
return true;
}
}
}

Here's an experimental version i've been tinkering with. I'm thinking using
Code:
pfsockopen()
would lend to further speed increases because of the ability to reuse a previously used socket. this would be good with ssl.

Code:
class jsonRPCSocket {
private $host;
private $port;
private $user;
private $pass;
private $id;
private $notification = false;
public function __construct($host,$port,$user,$pass) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->pass = $pass;
$this->id = 1;
}
public function setRPCNotification($notification) {
empty($notification) ? $this->notification = false : $this->notification = true;
}
public function __call($method,$params) {
if (!is_scalar($method)) { throw new Exception('Method name has no scalar value'); }             
if (is_array($params)) { $params = array_values($params);}else{ throw new Exception('Params must be given as array'); }
if ($this->notification) {$currentId = NULL; }else{ $currentId = $this->id;}
$request = array( 'method' => $method, 'params' => $params, 'id' => $currentId );
$request = json_encode($request);
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
$fp = fsockopen('tcp://'.$this->user.':'.$this->pass.'@'.$this->host,$this->port,$errno,$errstr, 10);
if(!$fp){
throw new Exception("$errno - $errstr");
}
$content = "POST $request HTTP/1.1"."\r\n".
"Host: ". $this-host .""."\r\n".
"Content-Type: application/application-json"."\r\n".
"Content-Length:".strlen($request)."\r\n"."\r\n";
fwrite($fp,$content);
$response='';
while(!feof($fp)) {
$response.= fread($fp,1024);
}
fclose($fp);
$response = json_decode(preg_replace('!^.*(?:\r?\n){2}(.*)$!s','\\1',$response));
if ($this->debug) { echo nl2br($debug); }
if (!$this->notification) {
if ($response['id'] != $currentId) { return $response; }
if (!is_null($response['error'])) { return $response; }
return $response['result'];
}else{
return true;
}
}
}

My negative trust rating is reflective of a personal vendetta by someone on default trust.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!