curl post 发送json

* post 发送json

Example#1:

** post.php

 '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:

 '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

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

使用火狐浏览器

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"

如果遇到https报错:

SSL certificate problem: unable to get local issuer certificate
OpenSSL was built without SSLv2 support

添加2行:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

你可能感兴趣的:(php,web,php,curl,post,json)