12

NOTE: How to use basic authorization in PHP curl Does not work for me.

Trying to Curl into Electrum, but it looks like my access is denied. Electrum is on the same server as the php script. I have purposefully left out any command for the wallet for simplification purpose. Focusing on connecting first.

When trying to CURL in terminal

curl --data-binary '{"id":"curltext","method":"addrequest","params":{"amount":"3.14","memo":"test"}}' http://user:pass@127.0.0.1:7777

Error Message

curl: (7) Failed to connect to 127.0.0.1 port 7777: Connection refused

EDIT: The above CURL Command in Terminal now works

PHP

$password = 'root';         
$username = 'password';
$URL='http://127.0.0.1';   //Curl into own machine local ip is needed.
$port = 7777;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result=curl_exec($ch);

if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   

curl_close ($ch);

echo "<br>";
var_dump($result);
echo "<br>";
var_dump($status_code);

Below is my error

Error:

bool(false)

int(0)

Below are my netstats. Port is open and listening.

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:7777          0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN           

PHP7, PHP-CURL & CURL are installed on linux Fedora 27. Webserver type Apache 2.4 Electrum 3.1.2

curl -v 127.0.0.1 7777

Returns:

Rebuilt URL to: 127.0.0.1/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.55.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 04 Apr 2018 18:22:14 GMT
< Server: Apache/2.4.29 (Fedora)
< X-Powered-By: PHP/7.1.15
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
< 

/*index.php code*/

* Connection #0 to host 127.0.0.1 left intact
* Rebuilt URL to: 7777/
*   Trying 0.0.30.97...
* TCP_NODELAY set
* Immediate connect fail for 0.0.30.97: Invalid argument
* Closing connection 1
curl: (7) Couldn't connect to server
seamus
  • 2,681
  • 7
  • 26
  • 49
  • Try enabling curl verbose output and see if that gives you a clue: `curl_setopt($ch, CURLOPT_VERBOSE, true);` – eyevan Apr 04 '18 at 13:50
  • There is no ssl – seamus Apr 04 '18 at 13:51
  • 1
    Leave PHP aside for now, can you get it to work with curl command line? – Tarun Lalwani Apr 04 '18 at 15:28
  • [root@fedora-e ~]# curl --data-binary '{"id":"curltext","method":"addrequest","params":{"amount":"3.14","memo":"test"}}' http://username:password@127.0.0.1:7777 curl: (7) Failed to connect to 127.0.0.1 port 7777: Connection refused – seamus Apr 04 '18 at 15:42
  • what is your Electrum **version** ? – Paolo Apr 04 '18 at 15:55
  • do `curl -v 127.0.0.1 7777` it provides verbose mode. It looks that there is a listener, but, that doesn't ensure you get a reply. Check the Electrum backend. – Evhz Apr 04 '18 at 16:02
  • Electrum Version 3.1.2 – seamus Apr 04 '18 at 18:20
  • curl -v 127.0.0.1 7777 returns: view edit – seamus Apr 04 '18 at 18:20
  • 2
    looks to me like Electrum was designed by idiots - the api doesn't support encryption, listens to connections from anywhere (not only 127.0.0.1), and uses HTTP Basic Auth, meaning the username/password is sent in plain text on every api request, for any hacker listening in on the network (Wireshark?), a horribly insecure setup... – hanshenrik Apr 06 '18 at 05:38
  • 1
    You have password and username switched around. `$password = 'root';` Shouldn't the user name be `root`? This might just be a typo because you removed the sensitive data, but if you have them mixed up, this might explain your problem :-) – masterfloda Apr 10 '18 at 23:57
  • I know I'm just throwing in things that you have probably tried and considered before, but I want to make sure it's nothing trivial that got overlooked. Have you tried setting the `CURLOPT_RETURNTRANSFER` option? What is the value of `curl_errno($ch)` (as it doesn't seem to be `0`)? What happens if you dump the whole `curl_getinfo($ch)` (without specifying an option)? – masterfloda Apr 11 '18 at 00:10
  • Please make sure you have port 7777 available. – Sweet Chilly Philly Apr 11 '18 at 04:11
  • The remote server might be unfriendly to automation looking user agents. Try setting the user agent header to something generic, like Google's user agent. – Anthony Apr 11 '18 at 11:32
  • 1
    What does `curl -v 127.0.0.1:7777` output? The current variant (with a space) is useless – Sjon Apr 11 '18 at 13:48

1 Answers1

0

You need another curl option set:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

So the final sequence would be:

$password = 'root';
$username = 'password';
$URL='http://127.0.0.1';   //Curl into own machine local ip is needed.
$port = 7777;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");


$result=curl_exec($ch);

if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close ($ch);

echo "<br>";
var_dump($result);
echo "<br>";
var_dump($status_code);
Ryan Wright
  • 177
  • 1
  • 10