• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用curl post数据有关问题

php 搞代码 3年前 (2022-01-23) 29次浏览 已收录 0个评论

使用curl post数据问题
最近遇到一个很奇怪的问题
在发送post请求时,如果POSTFIELDS的类型为string的话,就会超时,但类型为array就没问题。

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);// 数据为string类型时,超时;// 如果是array('param' => $data_string)就没问题。curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_TIMEOUT, 3);curl_exec($ch);curl_close($ch);

——解决方案——————–
传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded。
这是它们唯一的区别,超时是网络因素我觉得,和代码无关
另外你的TIMEOUT调的的确有点小了,超时就像家常便饭
——解决方案——————–
那你的string是什么?
——解决方案——————–
网上找些列子看看,是不是用法不对。如果都正确,只能说明是其它原因导致的

http://hi.baidu.com/chenxiandong1988/blog/item/56f3b0282bd0fde6e7cd4087.html
——解决方案——————–
我用你的代码在本地试了一下,并没有发现问题。

PHP code

$data_string = "ispost=ok&msg=post";$data = array("ispost"=>"ok","msg"=>"post");$ch = curl_init();curl_setopt($ch, CU本&文来源gao@daima#com搞(%代@#码网@RLOPT_URL, "http://localhost/my/www/getdata.php");curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_TIMEOUT, 30);$result = curl_exec($ch);curl_close($ch);echo $result; 


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用curl post数据有关问题
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址