Chinaunix

标题: 用php建立socket使用http发送post请求到aspx页面,返回500错误. [打印本页]

作者: brucecheng    时间: 2003-10-10 18:59
标题: 用php建立socket使用http发送post请求到aspx页面,返回500错误.
人家给我们做了一个接口,要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错误.
作者: brucecheng    时间: 2003-10-10 20:18
标题: 用php建立socket使用http发送post请求到aspx页面,返回500错误.
没有人会?还是没有人看呀..着急呀.一直在线等........................
作者: brucecheng    时间: 2003-10-11 00:16
标题: 用php建立socket使用http发送post请求到aspx页面,返回500错误.
upupupupupupupup
作者: brucecheng    时间: 2003-10-11 09:17
标题: 用php建立socket使用http发送post请求到aspx页面,返回500错误.
555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
作者: brucecheng    时间: 2003-10-11 14:48
标题: 用php建立socket使用http发送post请求到aspx页面,返回500错误.
问题解决了.唉...........................................................
作者: czd327917086    时间: 2013-06-04 11:15
怎么解决的。回复 5# brucecheng


   
作者: ydlhero    时间: 2013-06-04 16:04
尝试用 curl 函数看看




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2