关于异步Http post,为什么curl可以,fsockopen+fputs却不行?

BasicArchitect 2015-06-09 11:01:49
下面两段代码是关于异步httppost的,为什么curl可以成功.
但是用sock的方式却失败(偶尔会成功一次)

$param = array(
'college_id'=>$college_id,
'order_id'=>$order_id
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$info = curl_exec($ch);
if (curl_errno($ch)) {
$this->ajaxReturn('Errno'.curl_error($ch) ,'JSON');
}
curl_close($ch);


fsockopen+fputs方式

ignore_user_abort(true); // 忽略客户端断开
set_time_limit(0); // 设置执行不超时

$urlinfo = parse_url($url);

$host = $urlinfo['host'];
$path = $urlinfo['path'];
$query = isset($param)? http_build_query($param) : '';

$port = 80;
$errno = 0;
$errstr = '';
$timeout = 10;

$fp = fsockopen($host, $port, $errno, $errstr, $timeout);

if(!$fp) {
$this->ajaxReturn( $errstr ,'JSON');
} else {
$out = "POST ".$path." HTTP/1.1\r\n";
$out .= "host:".$host."\r\n";
$out .= "content-length:".strlen($query)."\r\n";
$out .= "content-type:application/x-www-form-urlencoded; charset=UTF-8\r\n";
$out .= "connection:close\r\n\r\n";
$out .= $query;
$ret=fputs($fp, $out);
$this->ajaxReturn( $out ,'JSON');
fclose($fp);
}


...全文
270 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BasicArchitect 2015-06-11
  • 打赏
  • 举报
回复
引用 4 楼 fdipzone 的回复:
但是用sock的方式却失败(偶尔会成功一次) 失败时有什么错误提示?
什么提示也没有,所有函数返回值正常, 但实际上表单没Post。
傲雪星枫 2015-06-11
  • 打赏
  • 举报
回复
那就奇怪了,可以联系sae客服问问。
傲雪星枫 2015-06-10
  • 打赏
  • 举报
回复
但是用sock的方式却失败(偶尔会成功一次) 失败时有什么错误提示?
BasicArchitect 2015-06-10
  • 打赏
  • 举报
回复
引用 2 楼 fdipzone 的回复:
SAE有些方法是屏蔽的。你要看看是否调用了这些方法,需要改为sae提供的方法。 禁用的函数:http://sae.sina.com.cn/doc/php/runtime.html
没有那些禁用的函数。 就是上面我给出的那段代码,运行没有错误和异常。 但是就是提交不了表单。 你有SAE账户么?可以帮试试看么?
BasicArchitect 2015-06-09
  • 打赏
  • 举报
回复
好像问题找到了, 我原来部署到SAE上调试的,一直有问题. 现在部署到本地服务器上,就没问题了. 问题来了,为什么在SAE上不好用?
傲雪星枫 2015-06-09
  • 打赏
  • 举报
回复
SAE有些方法是屏蔽的。你要看看是否调用了这些方法,需要改为sae提供的方法。 禁用的函数:http://sae.sina.com.cn/doc/php/runtime.html

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧