简单的一个http的post

brucecheng 2003-10-10 08:30:45
人家给我们做了一个接口,要ip绑定的,很简单的一个http的post.结果我的总是返回500错误.
代码是:
<?
function posttohost($url, $data)
{
$url = parse_url($url);
if (!$url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; }

$encoded = "";

while (list($k,$v) = each($data))
{
$encoded .= ($encoded ? "&" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}

$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
if (!$fp) return "Failed to open socket to $url[host]";

fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($encoded) . "\n");
fputs($fp, "Connection: close\n\n");

fputs($fp, "$encoded\n");

$line = fgets($fp,1024);
if (!eregi("^HTTP/1\\.. 200", $line)) return $line ;

$results = ""; $inheader = 1;
while(!feof($fp))
{
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);

return $results;
}


$data=array();
$data["msg"]="HELLO THIS IS TEST MSG";
$data["Type"]="TEXT";
echo posttohost("Http://url/xxx.aspx", $data);
?>



结果返回的是HTTP/1.1 500 Internal Server Error

如果没有绑定ip的机器执行的话直接是404错误.
...全文
33 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
brucecheng 2003-10-11
  • 打赏
  • 举报
回复
up
brucecheng 2003-10-11
  • 打赏
  • 举报
回复
但是用没有绑定的ip去提交的话是404错误.找不到页面
xuzuning 2003-10-11
  • 打赏
  • 举报
回复
没有说你的代码有错。既然“要ip绑定”,那么问题在ip上。你的ip和“他们的服务器”上的ip总不相同吧?
brucecheng 2003-10-11
  • 打赏
  • 举报
回复
但是同样参数字符串直接在他们的服务器上用ie提交就没有问题
xuzuning 2003-10-11
  • 打赏
  • 举报
回复
500是拒绝服务吧?可能是参数有误
brucecheng 2003-10-11
  • 打赏
  • 举报
回复
5555555555555555555555555555555
brucecheng 2003-10-10
  • 打赏
  • 举报
回复
当然是支持的,如果不支持怎么会返回这个错误呢.

同时,我用这个函数,post数据给同样机器的php页面都能返回正常.
lapmx 2003-10-10
  • 打赏
  • 举报
回复
编译PHP时支持fsockopen吗?

21,886

社区成员

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

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