能不能说的具体一点怎么传,并且还有一个Content-Length不知道该怎么办。

解决方案 »

  1.   

    Content-Length就是报文的串长度.报文报头格式都给你了,套入socket请求即可.
    用csdn的webservice当例子.
    调用方法的格式说明在这里
    http://forum.csdn.net/OpenApi/forumapi.asmx?op=GetUserProfile
    $user  = '你的csdn帐号';
    $pass  = '帐号密码';
    $friend = '你的csdn帐号';//要查询的帐号

    $fn = fsockopen('forum.csdn.net',80,$errstr,$errno,1);
    if(!$fn)
    {
    exit($errstr);
    }
    $post = <<<POST
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetUserProfile xmlns="http://www.csdn.net/">
          <identity>
            <username>{$user}</username>
            <password>{$pass}</password>
          </identity>
          <username>{$friend}</username>
        </GetUserProfile>
      </soap:Body>
    </soap:Envelope>
    POST;
    fwrite($fn,"POST /OpenApi/forumapi.asmx HTTP/1.1\r\n");
    fwrite($fn,"Host: forum.csdn.net\r\n");
    fwrite($fn,"Content-Type: text/xml; charset=utf-8\r\n");
    fwrite($fn,"Connection:close\r\n");
    fwrite($fn,"Content-Length:".strlen($post)."\r\n\r\n");

    fwrite($fn,$post);
    while(!feof($fn))
    {
    $return .= fgets($fn,1024);
    }
    fclose($fn);

    echo "<xmp>".$return."</xmp>";//帐户的信息,xml格式
      

  2.   

    用curl更好,content-length头会自动生成.
      

  3.   

    谢谢解答,我照着你的代码改了改,结果出错了。
    错误原因是:Server was unable to process request. ---&gt; Name cannot begin with the '&lt;' character, hexadecimal value 0x3C. Line 1, position 2.
    能不能帮忙看下错在哪,我觉得变量的名字没问题啊代码如下
     $username='wanger';
            $password='123456abc!';
            $ToITPhoneNo='00653218569745';
            
            $fn = fsockopen('www.internettelecom.com.sg',80,$errstr,$errno,1);
            if(!$fn)
            {
                exit($errstr);    
            }
            $post = "<<
    <?xml version=\"1.0\" encoding=\"utf-8\"?>
    <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
      <soap:Body>
        <GetLogin xmlns=\"http://www.internettelecom.com.sg/IPhoneWS/\">
          <UserName>".$username."</UserName>
          <Password>".$password."</Password>
          <ToITPhoneNo>".$ToITPhoneNo."</ToITPhoneNo>
        </GetLogin>
      </soap:Body>
    </soap:Envelope>";        fwrite($fn,"POST /IPhoneWS/IPhoneWebService.asmx HTTP/1.1\r\n");
            fwrite($fn,"Host: www.internettelecom.com.sg\r\n");
            fwrite($fn,"Content-Type: text/xml; charset=utf-8\r\n");
            fwrite($fn,"Connection:close\r\n");
            fwrite($fn,"Content-Length:".strlen($post)."\r\n\r\n");
        
            fwrite($fn,$post);
            while(!feof($fn))
            {
                $return .= fgets($fn,1024);    
            }
            fclose($fn);
            
            echo "<xmp>".$return."</xmp>";
      

  4.   

    注意细节问题 $username='wanger';
            $password='123456abc!';
            $ToITPhoneNo='00653218569745'; $fn = fsockopen('www.internettelecom.com.sg',80,$errstr,$errno,1);
    if(!$fn)
    {
    exit($errstr);
    }
    //你的$post = "<<...";明显是有问题的.
    $post = <<<POST
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetLogin xmlns="http://www.internettelecom.com.sg/IPhoneWS/">
          <UserName>{$username}</UserName>
          <Password>{$password}</Password>
          <ToITPhoneNo>{$ToITPhoneNo}</ToITPhoneNo>
        </GetLogin>
      </soap:Body>
    </soap:Envelope>
    POST;
    fwrite($fn,"POST /IPhoneWS/IPhoneWebService.asmx HTTP/1.1\r\n");
    fwrite($fn,"Host: www.internettelecom.com.sg\r\n");
    fwrite($fn,"Content-Type: text/xml; charset=utf-8\r\n");
    fwrite($fn,"Connection:close\r\n");
    fwrite($fn,"Content-Length:".strlen($post)."\r\n\r\n");

    fwrite($fn,$post);
    while(!feof($fn))
    {
    $return .= fgets($fn,1024);
    }
    fclose($fn);

    echo "<xmp>".$return."</xmp>";
    按照以上代码,可以返回GetLoginResponse,接下来是你调用方法的参数问题,自己去查使用手册,或者问webservice的提供商
    http response :HTTP/1.1 200 OK
    Server: Microsoft-IIS/5.0
    Date: Mon, 17 May 2010 10:52:24 GMT
    X-Powered-By: ASP.NET
    Connection: close
    X-AspNet-Version: 2.0.50727
    Cache-Control: private, max-age=0
    Content-Type: text/xml; charset=utf-8
    Content-Length: 373<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetLoginResponse xmlns="http://www.internettelecom.com.sg/IPhoneWS/"><GetLoginResult>err02</GetLoginResult></GetLoginResponse></soap:Body></soap:Envelope>
      

  5.   

    得到的数据应该怎么读取?比如我要读取GetLoginResult的值,是不是用读取xml的方法?我用下面的代码来读取,结果什么也没得到。。
    $xml = new DOMDocument();
    $xml->load($rs);
    $envelope = $xml->getElementsByTagName( "soap:Envelope" );$body = $envelope->getElementsByTagName( "soap:Body" );

    $GetLoginResponse = $body->getElementsByTagName( "GetLoginResponse" );$GetLoginResult = $GetLoginResponse->getElementsByTagName( "GetLoginResult" );if( strcmp($GetLoginResult,"err02")==0 ){
    echo "00000000000";
    }else{
    echo "11111111111";
    }
      

  6.   

    上面的$rs="<xmp>".$return."</xmp>";