在PHP脚本中的CENTOS 7 php5(cli)和curl模块中,我尝试在php脚本中使用REST API制作POST(JSON),并且在我的CENTOS 7主机上使用PHP得到了一个奇怪的行为。
以下是脚本的输出:
[+] API GLPI : Session Initializing
User Token ===> YsYJnd81WFiFIJ0pqdW6GqiujUpfkH12tdlgYElj
Init Url ===> http://192.168.158.129:8080/apirest.php/initSession/* About to
connect() to 192.168.158.129 port 8080 (#0)
* Trying 192.168.158.129...
* Connected to 192.168.158.129 (192.168.158.129) port 8080 (#0)
POST /apirest.php/initSession/ HTTP/1.1
Host: 192.168.158.129:8080
Accept: */*
Content-Type: application/json
Authorization: user_token YsYJnd81WFiFIJ0pqdW6GqiujUpfkH12tdlgYElj
Content-Length: -1
Expect: 100-continue
注意:Content-Lenght设置为-1
服务器返回
HTTP / 1.1 400错误请求
没有大块,没有接近,没有大小。假设接近信号结束
关闭连接0
清漆日志输出
varnish | * << Request >> 32777
varnish | - Begin req 32776 rxreq
varnish | - Timestamp Start: 1522946173.840342 0.000000 0.000000
varnish | - Timestamp Req: 1522946173.840342 0.000000 0.000000
varnish | - HttpGarbage "POST%00"
varnish | - ReqAcct 226 0 226 28 0 28
varnish | - End
varnish |
varnish | * << Session >> 32776
varnish | - Begin sess 0 HTTP/1
varnish | - SessOpen 192.168.158.129 35336 :6081 172.26.0.11 6081 1522946173.840172 20
varnish | - Link req 32777 rxreq
varnish | - SessClose RX_JUNK 0.000
varnish | - End
varnish |
注意:HttpGarbage“POST%00”
uname -a
Linux localhost.localdomain 3.10.0-693.17.1.el7.x86_64 #1 SMP Thu Jan 25
20:13:58 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
rpm -qa | grep -i php
php-common-5.4.16-43.el7_4.1.x86_64
php-process-5.4.16-43.el7_4.1.x86_64
php-xml-5.4.16-43.el7_4.1.x86_64
php-pecl-json-post-1.0.0-2.el7.x86_64
php-cli-5.4.16-43.el7_4.1.x86_64
php-pear-Net-Curl-1.2.5-10.el7.noarch
php-pear-1.9.4-21.el7.noarch
php-mbstring-5.4.16-43.el7_4.1.x86_64
php -v
PHP 5.4.16 (cli) (built: Mar 7 2018 13:34:47)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
rpm -qa | grep -i curl
libcurl-devel-7.29.0-42.el7_4.1.x86_64
libcurl-7.29.0-42.el7_4.1.x86_64
curl-7.29.0-42.el7_4.1.x86_64
python-pycurl-7.19.0-19.el7.x86_64
php-pear-Net-Curl-1.2.5-10.el7.noarch
php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
json_post
libxml
mbstring
mhash
openssl
pcntl
pcre
Phar
posix
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zip
zlib
[+] API GLPI : Session Initializing
User Token ===> YsYJnd81WFiFIJ0pqdW6GqiujUpfkH12tdlgYElj
Init Url ===> http://192.168.158.129:8080/apirest.php/initSession/{"session_token":"nmoa5clfg0mfn8g5i153o8iqep"}
Array
( [session_token] => nmoa5clfg0mfn8g5i153o8iqep )
[+] SESSION TOKEN ===> nmoa5clfg0mfn8g5i153o8iqep
[+] API GLPI : TICKET CREATION
{"id":9,"message":"Your ticket has been registered, its treatment is in progress. (Ticket: 9)"}* Hostname was NOT found in DNS cache
* Trying 192.168.158.129...
* Connected to 192.168.158.129 (192.168.158.129) port 8080 (#0)
POST /apirest.php/initSession/ HTTP/1.1
Host: 192.168.158.129:8080
Accept: */*
Content-Type: application/json
Authorization: user_token YsYJnd81WFiFIJ0pqdW6GqiujUpfkH12tdlgYElj
Expect: 100-continue
请注意:No Strange Content-Lenght设置为-1
然后服务器响应
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 05 Apr 2018 13:08:15 GMT
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
apachehost: 019f184d31ff
Content-Length: 46
Content-Type: application/json; charset=UTF-8
X-Cacheable: NO:Cache-Control=no-store
X-Varnish: 32772
Age: 0
Via: 1.1 varnish (Varnish/5.0)
X-Cache: MISS
Accept-Ranges: bytes
Connection: keep-alive
Connection #0 to host 192.168.158.129 left intact
清漆日志
varnish | * << BeReq >> 16
varnish | - Begin bereq 15 pass
varnish | - Timestamp Start: 1522946334.370154 0.000000 0.000000
varnish | - BereqMethod POST
varnish | - BereqURL /apirest.php/initSession/
varnish | - BereqProtocol HTTP/1.1
varnish | - BereqHeader Host: 192.168.158.129:8080
varnish | - BereqHeader Accept: */*
varnish | - BereqHeader Content-Type: application/json
varnish | - BereqHeader Authorization: user_token YsYJnd81WFiFIJ0pqdW6GqiujUpfkH12tdlgYElj
varnish | - BereqHeader X-Forwarded-For: 192.168.158.1
varnish | - BereqHeader X-Varnish: 16
varnish | - VCL_call BACKEND_FETCH
varnish | - VCL_return fetch
varnish | - BackendOpen 23 boot.NODE019f184d31ff 172.26.0.10 80 172.26.0.11 48398
varnish | - BackendStart 172.26.0.10 80
varnish | - Timestamp Bereq: 1522946334.370492 0.000338 0.000338
varnish | - Timestamp Beresp: 1522946334.480720 0.110566 0.110228
varnish | - BerespProtocol HTTP/1.1
varnish | - BerespStatus 200
varnish | - BerespReason OK
varnish | - BerespHeader Date: Thu, 05 Apr 2018 16:38:54 GMT
varnish | - BerespHeader Server: Apache/2.4.27 (Ubuntu)
(....)
配置
$ php-win.exe -v
PHP 5.5.12 (cli) (built: Apr 30 2014 11:21:04)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
php-win.exe -m
[PHP Modules]
bcmath
bz2
calendar
com_dotnet
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
imap
intl
json
ldap
libxml
mbstring
mcrypt
mhash
mysql
mysqli
mysqlnd
odbc
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
#1st request to get a Session Token
$api_url="http://192.168.158.129:8080/apirest.php";
$usertoken="YsYJnd81WFiFIJ0pqdW6GqiujUpfkH12tdlgYElj";
echo " [+] API GLPI : Session Initializing ";
echo (" User Token ===> " . $usertoken);
$headers = array(
('Content-Type: application/json'),
('Authorization: user_token ' . $usertoken)
);
$initurl=$api_url."/initSession/";
echo ("\n Init Url ===> " . $initurl);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $initurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$request_result = curl_exec($ch);
echo $request_result;
curl_close($ch);
$obj = json_decode($request_result,true);
print_r ($obj);
$session_token="";
$session_token=$obj['session_token'];
if ($session_token == ""){
echo "FATAL ! Error to retrieve Session Token";
exit(2);
}
echo (" [+] SESSION TOKEN ===> " . $session_token);
....
# Then 2nd request to Post JSON not shown here
答案 0 :(得分:0)
向POST添加有效负载,因为如果没有帖子内容,curl将添加-1内容长度,具体取决于您使用的curl版本。更高版本将添加0长度内容长度标头,这可能是您看到不同机器之间的变化的原因。
$payloadJson = json_encode([]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadJson);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
"Content-type: application/json",
"Content-Length: ".strlen($payloadJson),
"Authorization: user_token " . $usertoken,
]
);
答案 1 :(得分:0)
我发现我的错误让我感到羞耻。在我设置的第一个请求中
curl_setopt($ch, CURLOPT_POST, 0);
虽然我没有POST数据!解决问题,将其更改为
eclipse.ini
-vmargs
-Dosgi.requiredJavaVersion=1.8
然而,它表明我们应该非常小心这个卷曲库,因为它可以有显着差异