注意: How to use basic authorization in PHP curl 对我不起作用。
尝试卷入Electrum,但看起来我的访问被拒绝了。 Electrum与php脚本位于同一服务器上。为了简化目的,我故意遗漏了钱包的任何命令。专注于先连接。
在终端
中尝试CURL时curl --data-binary '{"id":"curltext","method":"addrequest","params":{"amount":"3.14","memo":"test"}}' http://user:pass@127.0.0.1:7777
错误消息
curl: (7) Failed to connect to 127.0.0.1 port 7777: Connection refused
编辑:终端中的上述CURL命令现在可以正常工作
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);
以下是我的错误
错误:
布尔(假)
INT(0)
以下是我的netstats。港口是开放和倾听的。
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
安装在Linux Fedora 27上.Webserver类型Apache 2.4
Electrum 3.1.2
curl -v 127.0.0.1 7777
返回:
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
答案 0 :(得分:0)
您需要设置另一个卷曲选项:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
所以最后的序列是:
$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);