今天在做一个api增量的功能的时候出现了一个特别奇怪的问题,我用curl 想tomcat post数据的时候,tomcat竟然报错,所我post的数据没有正确获得,但是,我用curl post给我自己写的一个页面,就可以在$_post数组中获得数据.
为什么会出现这种问题那?
原因是在构建post 数据的数量出现问题,代码如下:
- function api_notice_increment($url, $data) {
- $ch = curl_init();
- curl_setopt($ch, curlopt_header,0);
- curl_setopt($ch, curlopt_returntransfer, 1);
- curl_setopt($ch, curlopt_url, $url);
- curl_setopt($ch, curlopt_post, 1);
- curl_setopt($ch, curlopt_postfields, $data);
- $lst['rst'] = curl_exec($ch);
- $lst['info'] = curl_getinfo($ch);
- curl_close($ch);
- return $lst;
-
- $url = "http://localhost/test/post.api.php?app=test&act=testact";
- $data = array (
- 'goods_id' => '1010000001224',
- 'store_id' => '20708',
- 'status' => 'goodsdownshelf',
- ;
post.api.php的代码:
<?phperror_log(var_export($_post,1),3,'d:/post.txt');
执行上面的代码,在我的d:/生成的post.txt文件,其内容如下:
- array (
- 'goods_id' => '1010000001224',
- 'store_id' => '20708',
- 'status' => 'goodsdownshelf',
- )
说明post的数据可以正常的获得,修改上的代码:
- function api_notice_increment($url, $data)
- $ch = curl_init();
- curl_setopt($ch, curlopt_header,0);
- curl_setopt($ch, curlopt_returntransfer, 1);
- curl_setopt($ch, curlopt_url, $url);
- curl_setopt($ch, curlopt_post, 1);
- $data = http_build_query($data);
- curl_setopt($ch, curlopt_postfields, $data);
- $lst['rst'] = curl_exec($ch);
- $lst['info'] = curl_getinfo($ch);
- curl_close($ch);
- return $lst;
-
- $url = "http://localhost/test/post.api.php?app=test&act=testact";
- $data = array (
- 'goods_id' => '1010000001224',
- 'store_id' => '20708',
- 'status' => 'goodsdownshelf',
-
- api_notice_increment($url,$data);
只是在执行 curl_setopt($ch, curlopt_postfields, $data);之前执行下$data = http_build_query($data);操作.
删除d:/post.txt文件,再次运行,再次打开d:/post.txt文件,其内容如下:
- array (
- 'goods_id' => '1010000001224',
- 'store_id' => '20708',
- 'status' => 'goodsdownshelf',
- )
如果不将$data 进行http_build_query的话,java代码就不能获得post的数据,http_build_query以后就可以正常的获得了. |