解决这个问题,通常的解决方式是这样的:
$jsonData = $_POST[‘jsonstr’];
此方式需要客户端提交一个jsonstr的参数过来,里面含的数据是json字符串,然后在服务端解析。
如果客户端直接传json数据过来,而没有上述jsonstr的参数。使用$_GET,$_POST 和 $_REQUEST是获取不到数据的。
解决的办法就是使用使用$GLOBALS这个全局变量,$GLOBALS[‘HTTP_RAW_POST_DATA’]就能获取客户端传来的json数据了。
下面给出thinkphp中控制器的使用curl提交数据代码:
class CcAction extends Action {
public function getjson(){
echo json_encode($GLOBALS['HTTP_RAW_POST_DATA']);//这里就能获取到testcurl传来的$json_string
}
public function testcurl(){
$header = array('Accept:application/json', 'Content-Type:application/json');
$patoken = array(
'name' => 'phpjyz',
'age' => '23',
);
$json_string = json_encode($patoken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_URL, 'http://www.scutephp.com/api.php/cc/getjson');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$ret = curl_exec($ch);
print_r(json_decode($ret, true));
}
}
返回的数据和$json_string是一模一样的。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号