php - 无法将 Curl-php 放入同一服务器 Electrum

标签 php curl

注意: 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);

下面是我的错误

Error:

bool(false)

int(0)

下面是我的网络统计。端口已打开并正在监听。

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 上。网络服务器类型 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

最佳答案

您需要另一个 curl 选项集:

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);

关于php - 无法将 Curl-php 放入同一服务器 Electrum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49599378/

相关文章:

php - 我想复制我的 WordPress 网站,但如何处理数据库?

php - 正则表达式 : Unknown modifier '\'

web-services - cURL + HTTP 和 RESTful Web 服务有什么区别?

r - quantmod-SSL : unable to get local issuer certificate in R

rest - 向 Microsoft Sharepoint API 发出 curl 请求?

php - 在同一台服务器上调用 cURL 会导致问题

PHP 未初始化的对象属性

javascript - VideoJS 分辨率选择器在 Wordpress 中不起作用

php - 通过 ssl 下载非常慢的 Nginx

php - 如何从 curl 句柄中删除先前设置的请求 header 引用字段?