<p>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. </p>

<p>two things that will help you here:</p>

<p>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:</p>

<p><strong>name this JsonRPCClient.php</strong></p>

<pre><code>/*
 * Copyright 2007 Sergio Vaccaro &lt;sergio@inservibile.org&gt;
 * GNU GPL LICENSE
 * The object of this class are generic jsonRPC 1.0 clients
 * http://json-rpc.org/wiki/specification
 * @author sergio &lt;jsonrpcphp@inservibile.org&gt;
 */
 class jsonRPCClient {
private $debug;
private $url;
private $id;
private $notification = false;
public function __construct($url,$debug = false) {
    $this-&gt;url = $url;
    empty($proxy) ? $this-&gt;proxy = '' : $this-&gt;proxy = $proxy;
    empty($debug) ? $this-&gt;debug = false : $this-&gt;debug = true;
    $this-&gt;id = 1;
}
public function setRPCNotification($notification) {
    empty($notification) ? $this-&gt;notification = false : $this-&gt;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-&gt;notification) {$currentId = NULL; }else{ $currentId = $this-&gt;id;}
    $request = array( 'method' =&gt; $method, 'params' =&gt; $params, 'id' =&gt; $currentId );
    $request = json_encode($request);
    $this-&gt;debug &amp;&amp; $this-&gt;debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
    $ch = curl_init($this-&gt;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-&gt;debug) { echo nl2br($debug); }
    if (!$this-&gt;notification) {
            if ($response['id'] != $currentId) { return $response; }
            if (!is_null($response['error'])) { return $response; } 
            return $response['result'];
    }else{
            return true;
    }
}
}
</code></pre>

<p>a basic wallet.class.php for you to use</p>

<p><strong>name this wallet.class.php</strong></p>

<pre><code>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-&gt;ip        = $ip;
    $this-&gt;username  = $us;
    $this-&gt;password  = $pa;
    $this-&gt;port      = $po;
    $this-&gt;acronym   = $ac;
    $this-&gt;Client    = new jsonRPCClient('http://' . $this-&gt;username . ':' .$this-&gt;password . '@' . $this-&gt;ip . ':' . $this-&gt;port);
    return true;
}
}
</code></pre>

<p>now to use this functionality you simply do the following, lets call it <strong>test.php</strong></p>

<pre><code>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-&gt;Client-&gt;getinfo(); //try a basic rpc command
    echo '&lt;pre&gt;'; //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
}
</code></pre>

<p>hope this helped, and good luck with your script.</p>

CC BY-SA 3.0
2014-04-01T07:23:09.250Z
I use the same library
fefe
CC BY-SA 3.0
2014-04-01T22:47:55.223Z
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 information
r3wt
CC BY-SA 3.0
2014-04-02T12:02:51.133Z
compiling bitcoind from github resolved the issue
fefe
CC BY-SA 3.0
2014-03-31T22:27:01.690Z
2014-03-31T22:27:01.690Z
r3wt
<p>I try to connect locally  with bitcoin client using the API but I get 404 on request</p>

<pre><code> testnet=1
 server = 1
 rpcport=8332
 rpcuser=myuser
 rpcpassword=mypass
</code></pre>

<p>I was installing bitcoind on ubuntu server with <code>apt-get install bitcoind</code></p>

<p><strong>PHP API Call</strong></p>

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

<p>var_dump();</p>

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

<p><strong>Update</strong></p>

<p>Compiling bitcoind from github resolved the issue </p>

<api><json-rpc>
Can't connect locally with API
fefe
# Can't connect locally with API

(Asked by: username_0 on 2014-03-31)

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

**Tags:** api, json-rpc


---

## 1 answers

### Answer by username_1 on 2014-03-31. Score: 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 
 * GNU GPL LICENSE
 * The object of this class are generic jsonRPC 1.0 clients
 * http://json-rpc.org/wiki/specification
 * @author sergio 
 */
 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 ''; //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.

> **Comments:**
> **username_0**: I use the same library  
> **username_1**: 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 information  
> **username_0**: compiling bitcoind from github resolved the issue  

---

