php - CENTOS 7 - curl PHP5 cli 和无效的 Content-Length

标签 php json curl centos

在 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 Bad Request

no chunk, no close, no size. Assume close to signal end

Closing connection 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​​"

CENTOS配置

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

请注意:没有奇怪的 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

一段PHP代码

#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

最佳答案

向 POST 添加有效负载,因为如果没有帖子内容,curl 将根据您使用的 curl 版本添加 -1 内容长度。更高版本将添加一个 0 长度的内容长度 header ,这可能是您看到不同机器之间存在差异的原因。

$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,
    ]
);

关于php - CENTOS 7 - curl PHP5 cli 和无效的 Content-Length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49674447/

相关文章:

php - 使用范围解析运算符(::) 分配变量与 $this->variable 相同吗?

php - 如果 codeigniter 项目存在,如何通过浏览器访问文件夹?

php - MySQL - 跳过重复的 WordPress 条目

php - Paypal REST API,怎么办?

php - 为下面的查询创建索引的最佳方法是什么

java - 哪个更安全,为什么是 JSON 或 XML

python - JSON 序列化非字符串字典键

javascript - 下划线 isEqual 和 JSON

c++ - 将 curl 结果中的字节组解释为其他数据类型的方法

curl - 如何使用 cURL 读取 websocket 响应