我试图通过cURL命令行发布数据到php。在我看来,它正在工作,但接收页面只是显示一个空数组。
LAMP堆栈在Ubuntu 20.04 LTS, PHP是7.4。
这是我的命令行cURL, -v输出。
$ curl -v -d "name=rabbit" https://example.com/test1.php -H "connection: close"
* Trying XXX.XXX.XXX.XXX:443...
* TCP_NODELAY set
* Connected to example.com (XXX.XXX.XXX.XXX) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: CN=example.com
* start date: Oct 6 01:42:48 2021 GMT
* expire date: Jan 4 01:42:47 2022 GMT
* subjectAltName: host "example.com" matched cert's "example.com"
* issuer: C=US; O=Let's Encrypt; CN=R3
* SSL certificate verify ok.
> POST /test1.php HTTP/1.1
> Host: example.com
> User-Agent: curl/7.68.0
> Accept: */*
> connection: close
> Content-Length: 23
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 23 out of 23 bytes
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Thu, 07 Oct 2021 19:25:25 GMT
< Server: Apache/2.4.41 (Ubuntu)
< Vary: Accept-Encoding
< Content-Length: 81
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
array(1) {
["name"]=>
string(4) "rabbit"
}
* Closing connection 0
* TLSv1.3 (OUT), TLS alert, close notify (256):
测试页只是
<?php
var_dump($_POST);
?>
输出是否表明它成功地发布了数据?接收服务器上的日志文件显示200。Php-curl当然已经安装并启用了。我是否缺少依赖项或配置更改,我需要通过cURL接收POST ?
TIA
我以前没有在命令行中使用过curl
,所以花了一点时间来找到它,并弄清楚如何处理无数可用的选项。我创建了一个与您的test.php
脚本类似的文件,但为了说明正在处理POST请求,添加了一些更多的数据。
<?php
/*
The command issued on cmd line:
curl -v -d "name=geronimo&tribe=apache&wife=alope&hobby=alcohol" --cacert "c:/wwwroot/cacert.pem" -k https://sentinel/demo/curl-test.php
*/
ob_clean();
#new data
$_POST['date']=date( DATE_ATOM );
$_POST['ip']=$_SERVER['REMOTE_ADDR'];
#format & print output
exit( sprintf( '%1$s%2$s%1$s', str_repeat( PHP_EOL, 1 ), print_r( $_POST, true ) ) );
?>
然后在命令行上发出上述(&following)命令:
curl -v -d "name=geronimo&tribe=apache&wife=alope&hobby=alcohol" --cacert "c:/wwwroot/cacert.pem" -k https://sentinel/demo/curl-test.php
使用修改后的POST数据生成以下详细输出:
* About to connect() to sentinel port 443 (#0)
* Trying 192.168.0.57... connected
* successfully set certificate verify locations:
* CAfile: c:/wwwroot/cacert.pem
CApath: none
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using DHE-RSA-AES256-SHA
* Server certificate:
* subject: C=GB; ST=Angus; L=Forfar; O=TrekSafe; OU=Web Development; CN=sentinel; emailAddress=web@sentinel
* start date: 2021-04-09 07:09:17 GMT
* expire date: 2022-04-08 07:09:17 GMT
* subjectAltName: sentinel matched
* issuer: O=AO Kaspersky Lab; CN=Kaspersky Anti-Virus Personal Root Certificate
* SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> POST /demo/curl-test.php HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1
.2.5
> Host: sentinel
> Accept: */*
> Content-Length: 51
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 51 out of 51 bytes
< HTTP/1.1 200 OK
< Date: Fri, 08 Oct 2021 12:41:50 GMT
< Server: Apache/2.4.34 (Win64) OpenSSL/1.1.0i PHP/7.2.9
< Upgrade: h2,h2c
< Connection: Upgrade
< X-Powered-By: PHP/7.2.9
< Content-Length: 167
< Content-Type: text/html; charset=UTF-8
<
Array
(
[name] => geronimo
[tribe] => apache
[wife] => alope
[hobby] => alcohol
[date] => 2021-10-08T13:41:50+01:00
[ip] => 192.168.0.57
)
* Connection #0 to host sentinel left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
您可以在响应中看到修改后的POST数据,这些数据大致显示在原始代码中。事实上,有一个200
http响应代码应该告诉你的请求是成功的。