首页 > php教程 > PHP源码 > php curl post数据的问题

php curl post数据的问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-08 17:25:20
原创
980 人浏览过
<script>ec(2);</script>
 
今天在做一个api增量的功能的时候出现了一个特别奇怪的问题。我用curl 想tomcat post数据的时候,tomcat竟然报错,所我post的数据
没有正确或得。但是,我用curl post给我自己写的一个页面,就可以在$_post数组中获得数据。
为什么会出现这种问题那?
原因是在构建post 数据的数量出现问题。。。
<div>1 function api_notice_increment($url, $data)<br> 2 {<br> 3     $ch = curl_init();        <br> 4     curl_setopt($ch, curlopt_header,0);<br> 5     curl_setopt($ch, curlopt_returntransfer, 1);<br> 6 <br> 7     curl_setopt($ch, curlopt_url, $url);<br> 8     curl_setopt($ch, curlopt_post, 1);<br> 9     curl_setopt($ch, curlopt_postfields, $data);<br>10     $lst['rst'] = curl_exec($ch);<br>11      $lst['info'] = curl_getinfo($ch);<br>12     curl_close($ch);<br>13     return $lst;<br>14 }<br>15     $url = "http://localhost/test/post.api.php教程?app=test&act=testact";<br>16     $data = array (<br>17   'goods_id' => '1010000001224',<br>18   'store_id' => '20708',<br>19   'status' => 'goodsdownshelf',<br>20 );</div>
登录后复制
//post.api.php的代码
<div>
<?php <br />error_log(var_export($_post,1),3,'d:/post.txt');</div>
登录后复制
执行上面的代码,在我的d:/生成的post.txt文件,其内容如下:
array (
  'goods_id' => '1010000001224',
  'store_id' => '20708',
  'status' => 'goodsdownshelf',
)
说明post的数据可以正常的获得。
修改上的代码
<div>1 <?php <br /> 2  function api_notice_increment($url, $data)<br> 3 {<br> 4     $ch = curl_init();        <br> 5     curl_setopt($ch, curlopt_header,0);<br> 6     curl_setopt($ch, curlopt_returntransfer, 1);<br> 7 <br> 8     curl_setopt($ch, curlopt_url, $url);<br> 9     curl_setopt($ch, curlopt_post, 1);<br>10     $data = http_build_query($data);<br>11     curl_setopt($ch, curlopt_postfields, $data);<br>12     $lst['rst'] = curl_exec($ch);<br>13      $lst['info'] = curl_getinfo($ch);<br>14     curl_close($ch);<br>15     return $lst;<br>16 }<br>17     $url = "http://localhost/test/post.api.php?app=test&act=testact";<br>18     $data = array (<br>19   'goods_id' => '1010000001224',<br>20   'store_id' => '20708',<br>21   'status' => 'goodsdownshelf',<br>22 );<br>23 <br>24 <br>25     api_notice_increment($url,$data);</div>
登录后复制
只是在执行 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以后就可以正常的获得了。
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
curl 模拟登陆
来自于 1970-01-01 08:00:00
0
0
0
将cURL命令行转换为PHP cURL代码
来自于 1970-01-01 08:00:00
0
0
0
将命令行 cURL 转换为 PHP cURL
来自于 1970-01-01 08:00:00
0
0
0
php5.3与php5.5的curl的区别
来自于 1970-01-01 08:00:00
0
0
0
请教各位大神,php curl请求页面显示空白
来自于 1970-01-01 08:00:00
0
0
0
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板