您好,欢迎访问一九零五行业门户网

如何用PHP获取无字段的json数据

如何用php获取无字段的json数据1、如何用php获取json数据?解决这个问题,通常的解决方式是这样的:
$jsondata = $_post[‘jsonstr’];
此方式需要客户端提交一个jsonstr的参数过来,里面含的数据是json字符串,然后在服务端解析。
2、客户端直接传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是一模一样的。
其它类似信息

推荐信息