* post 发送json

Example#1:

** post.php

<?php

$ch = curl_init();

$headers = [
    "X-Requested-With: XMLHttpRequest",
    "Content-Type: application/json",
    "Cookie: etsessionid=2k4pjijh9h1vm3gi7h3669atp6;",
    "Origin: http://172.16.0.224:7102"
];

$postdata = <<<EOF
{"query":{"bool":{"must":[{"match_phrase":{"request":"/ask/"}},{"range":{"@timestamp":{"gte":"2019-01-22 00:00:00","lte":"2019-01-22 09:21:30","format":"yyyy-MM-dd HH:mm:ss"}}},{"range":{"request_time":{"gte":2}}}]}},"sort":[{"request_time":{"order":"desc"}}],"size":100}
EOF;

array_push($headers, sprintf("Content-Length: %d", strlen($postdata)));

curl_setopt_array($ch, [
    CURLOPT_URL => 'http://xx.xx.xx.xx:9200/nginx-2019.01.22/_search',
    CURLOPT_HEADER => 0,   // 不输出响应的http header
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_BINARYTRANSFER => 1,
    CURLOPT_SAFE_UPLOAD => 1,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $postdata
]);

$data = curl_exec($ch);
curl_close($ch);

echo $data.PHP_EOL;

php post.php >query.json

 

Example#2:

<?php

$ch = curl_init();

$headers = [
    "X-Requested-With: XMLHttpRequest",
    "Cookie: name1=value1; name2=value2;"
];

$postdata = "{\"school\":\"高中\",\"subVersionList\":[{\"subject\":\"物理\",\"version\":\"2019-2020年度高一物理同步提高上学新课改版\"}]}";

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://xxx.com/api/my/active/xxxx/',
    CURLOPT_HEADER => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_BINARYTRANSFER => 1,
    CURLOPT_SAFE_UPLOAD => 1,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $postdata
]);

$data = curl_exec($ch);
curl_close($ch);

echo $data.PHP_EOL;

 

$ php json.php
HTTP/1.1 200 OK
Server: Tengine
Date: Thu, 03 Jan 2019 01:32:11 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=30
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

27900612
 

* PHP定界符 多行字符串

<<<定界符(无$符)

(开头无空格)第一行

XXX

XXX

XX

(开头无空格)定界符(无$符)

 

<?php

$raw = <<<EOF
{"success":false,"failDesc":"\u90e8\u5206\u8bfe\u7a0b\u6fc0\u6d3b\u5931\u8d25","result":{"success":false,"failDesc":"\u5185\u90e8\u51fa\u9519(2)\uff0c\u8bf7\u7a0d\u5019\u91cd\u8bd5"},"value":{"cid":"s207760","activecode":"1198255906606417","title":"","mg":"{\"required\":[\"s206776\"],\"elective\":[]}"}}
EOF;
echo $raw.PHP_EOL;

 

使用火狐浏览器

curl post 发送json_JavaScript

curl "http://172.16.0.224:8031/index.php/admin/field/getField" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0" -H "Accept: application/json, text/plain, */*" -H "Accept-Language: ja,en-US;q=0.7,en;q=0.3" --compressed -H "Referer: http://172.16.0.224:8080/" -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -H "authKey: ca0618571c3663b1b86b1664b526f448" -H "sessionId: 5pnbu5jkr8eb02shbgjk9i56o5" -H "Origin: http://172.16.0.224:8080" -H "Connection: keep-alive" --data "types=crm_product&module=crm&action=index&controller=product"