PHP Webservice连接但在调用函数时未经授权的错误

时间:2012-07-25 05:20:35

标签: php web-services soap

我调用了一个Web服务,它成功连接并返回其方法, 但是当我打电话给其中一个功能时,它会发现未经授权的错误..

我的代码在这里

try { 
$service = new SoapClient("http://www.go-to-orbit.com/oos/solo/soapServer.php?wsdl");

$header = new SoapHeader('http://oncore.qubitwebtechnologies.com/', 'AuthorisationHeader', array('login' => "ONCORE",'password' => "ONCORE"), false);
$service->__setSoapHeaders(array($header));   

var_dump($service->__getFunctions());

$response = $service->__soapCall("getAllOnHand", array() );
}
catch (Exception $e)
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

print_r($response);

结果

    array(2) { [0]=> string(18) "Map getAllOnHand()" [1]=> string(33) "int getOnHand(string $partNumber)" } Caught exception: Unauthorized 

我尝试了2天但没有运气,任何人都可以救我

错误消息

Exception object(SoapFault)#4 (9) { ["message":protected]=> string(12) "Unauthorized" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(47) "/home/qubitweb/public_html/oncore/jumi/soap.php" ["line":protected]=> int(87) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(47) "/home/qubitweb/public_html/oncore/jumi/soap.php" ["line"]=> int(87) ["function"]=> string(10) "__soapCall" ["class"]=> string(10) "SoapClient" ["type"]=> string(2) "->" ["args"]=> array(2) { [0]=> string(12) "getAllOnHand" [1]=> array(0) { } } } } ["previous":"Exception":private]=> NULL ["faultstring"]=> string(12) "Unauthorized" ["faultcode"]=> string(6) "Sender" } 

1 个答案:

答案 0 :(得分:1)

class SOAPStruct
{
    function __construct($user, $pass) 
    {
        $this->username = $user;
        $this->password = $pass;
    }
}

$service = new SoapClient("http://www.go-to-orbit.com/oos/solo/soapServer.php?wsdl");

$auth = new SOAPStruct("ONCORE","ONCORE");

$header = new     SoapHeader("http://oncore.qubitwebtechnologies.com/","AuthHeader",$auth,false); 

$service->__setSoapHeaders($header);   

$response = $service->getAllOnHand();


var_dump( $response );

此代码现在可以使用,您需要将值设置为相同的键,如AuthHeader,用户名,密码。