免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2868 | 回复: 1
打印 上一主题 下一主题

php curl post数据的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-30 20:56 |只看该作者 |倒序浏览
php curl post数据的问题



今天在做一个api增量的功能的时候出现了一个特别奇怪的问题。我用curl 想tomcat post数据的时候,tomcat竟然报错,所我post的数据

没有正确或得。但是,我用curl post给我自己写的一个页面,就可以在$_POST数组中获得数据。

为什么会出现这种问题那?

原因是在构建post 数据的数量出现问题。。。
  1. 1 function api_notice_increment($url, $data)
  2. 2 {
  3. 3     $ch = curl_init();         
  4. 4     curl_setopt($ch, CURLOPT_HEADER,0);
  5. 5     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6. 6  
  7. 7     curl_setopt($ch, CURLOPT_URL, $url);
  8. 8     curl_setopt($ch, CURLOPT_POST, 1);
  9. 9     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  10. 10     $lst['rst'] = curl_exec($ch);
  11. 11      $lst['info'] = curl_getinfo($ch);
  12. 12     curl_close($ch);
  13. 13     return $lst;
  14. 14 }
  15. 15     $url = "http://localhost/test/post.api.php?app=test&act=testact";
  16. 16     $data = array (
  17. 17   'goods_id' => '1010000001224',
  18. 18   'store_id' => '20708',
  19. 19   'status' => 'GoodsDownshelf',
  20. 20 );
复制代码
//post.api.php的代码
  1. <?phperror_log(var_export($_POST,1),3,'d:/post.txt');
复制代码
执行上面的代码,在我的d:/生成的post.txt文件,其内容如下:
  1. array (
  2.   'goods_id' => '1010000001224',
  3.   'store_id' => '20708',
  4.   'status' => 'GoodsDownshelf',
  5. )
复制代码
说明post的数据可以正常的获得。

修改上的代码
  1. 1 <?php
  2. 2  function api_notice_increment($url, $data)
  3. 3 {
  4. 4     $ch = curl_init();         
  5. 5     curl_setopt($ch, CURLOPT_HEADER,0);
  6. 6     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  7. 7  
  8. 8     curl_setopt($ch, CURLOPT_URL, $url);
  9. 9     curl_setopt($ch, CURLOPT_POST, 1);
  10. 10     $data = http_build_query($data);
  11. 11     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  12. 12     $lst['rst'] = curl_exec($ch);
  13. 13      $lst['info'] = curl_getinfo($ch);
  14. 14     curl_close($ch);
  15. 15     return $lst;
  16. 16 }
  17. 17     $url = "http://localhost/test/post.api.php?app=test&act=testact";
  18. 18     $data = array (
  19. 19   'goods_id' => '1010000001224',
  20. 20   'store_id' => '20708',
  21. 21   'status' => 'GoodsDownshelf',
  22. 22 );
  23. 23
  24. 24
  25. 25     api_notice_increment($url,$data);
复制代码
只是在执行 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);之前执行下$data = http_build_query($data);操作。

删除d:/post.txt文件

再次运行。

再次打开d:/post.txt文件,其内容如下:
  1. array (
  2.   'goods_id' => '1010000001224',
  3.   'store_id' => '20708',
  4.   'status' => 'GoodsDownshelf',
  5. )
复制代码
如果不将$data 进行http_build_query的话,java代码就不能获得post的数据,http_build_query以后就可以正常的获得了。

论坛徽章:
0
2 [报告]
发表于 2011-03-30 23:32 |只看该作者
curl post fields 如果直接是一个数组变量, 那么提交时会使用 multipart form-data 格式, 估计这个Java服务器不支持.

post fields是个字符串时, curl会使用urlencode 格式post数据, 一般post都是这样.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP