Can't connect locally with API

-2

I try to connect locally with bitcoin client using the API but I get 404 on request

 testnet=1
 server = 1
 rpcport=8332
 rpcuser=myuser
 rpcpassword=mypass

I was installing bitcoind on ubuntu server with apt-get install bitcoind

PHP API Call

 $_credintials = array('btc' =>'http://myuser:mypass@127.0.0.1:8332');
 //or 
 $_credintials = array('btc' =>'http://myuser:mypass@127.0.0.1:18332');
 $this->load->library('jsonRPCClient');
 $this->bitcoin = new jsonRPCClient($this->_credintials['btc']);

var_dump();

Message: fopen(http://...@127.0.0.1:8332): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found 

Update

Compiling bitcoind from github resolved the issue

fefe

Posted 2014-03-31T20:25:32.947

Reputation: 109

Answers

0

first of all, you shouldn't be using fopen. second of all you you obviously don't have rpcallowip set to whatever your ip is.

two things that will help you here:

this modified jsonrpc written by Gweedo is a secure way to connect to rpc in php. it uses curl instead of fopen. additionally, i modified this to return arrays instead of throwing exceptions, allowing you to keep log errors from your scripts silently or ignore them all together:

name this JsonRPCClient.php

/*
 * Copyright 2007 Sergio Vaccaro <sergio@inservibile.org>
 * GNU GPL LICENSE
 * The object of this class are generic jsonRPC 1.0 clients
 * http://json-rpc.org/wiki/specification
 * @author sergio <jsonrpcphp@inservibile.org>
 */
 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;
    }
}
}

a basic wallet.class.php for you to use

name this wallet.class.php

class Wallet {  
public $ip;
public $port;
public $username;
public $password;
public $Client;
public $credentials;
function Wallet($credentials)
{
    foreach($credentials as $row)
    {
        $ip = $row["ip"];
        $us = $row["user"];
        $pa = $row["pass"];
        $po = $row["port"];
        $ac = $row["acr"];
    }
    $this->ip        = $ip;
    $this->username  = $us;
    $this->password  = $pa;
    $this->port      = $po;
    $this->acronym   = $ac;
    $this->Client    = new jsonRPCClient('http://' . $this->username . ':' .$this->password . '@' . $this->ip . ':' . $this->port);
    return true;
}
}

now to use this functionality you simply do the following, lets call it test.php

require_once 'jsonRPCClient.php';
require_once 'class.wallet.php';
$credentials = array();
$credentials["ip"]   = '127.0.0.1';//your ip
$credentials["user"] = 'foo';//your username
$credentials["pass"] = 'bar';//your password
$credentials["port"] = '1337';//your port
$credentials["acr"]  = 'btc';
$rpc = new Wallet($credentials);//this returns true if no error during connection.
if($rpc === true) {
    $info =$rpc->Client->getinfo(); //try a basic rpc command
    echo '<pre>'; //make sure the array is printed neatly to the screen
    print_r($info, false); //dump the info to the screen
}else{
    print_r($rpc, false);//should dump the error for you
}

hope this helped, and good luck with your script.

r3wt

Posted 2014-03-31T20:25:32.947

Reputation: 239

I use the same libraryfefe 2014-04-01T07:23:09.250

no you use the original library. the one i use has been modified to replace the insecure fopen with curl and also never disclose error informationr3wt 2014-04-01T22:47:55.223

compiling bitcoind from github resolved the issuefefe 2014-04-02T12:02:51.133