研究了3天了,有个接口,需要提交格式如下:
name: "test",
password: "test",
{
"orderInfo":{
"title":"test",
},
"orderReceipt":{
"title":"test",
},
"title":"test"
}
我直接post这个字符串不行,但我post数组可以验证name和password,但json信息无法验证,如下:
Array
(
[name] => test
[password] => test
[0] => {
"orderInfo":{
"title":"test",
},
"orderReceipt":{
"title":"test",
},
"title":"test"
}
)
参考PHP手册选项CURLOPT_POSTFIELDS:https://www.php.net/manual/zh/function.curl-setopt.php
使用Guzzle请求,参考Guzzle文档:https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html#id9
public function curlHttpPost($url, $data, $header = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$sResult = curl_exec($ch);
curl_close($ch);
return $sResult;
}
function post_data($url,$data) {
//* 来源 https://www.bz80.vip
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.bz80.com');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1');
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($data)
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $return_content;
}