首页 文章

PHP SoapClient - 未经授权的操作异常 - 请求是否正确形成?

提问于
浏览
1

我正在尝试访问第三方GPS跟踪SOAP WebService以返回我们公司车辆的列表 . 我一直在浏览SoapClient对象的文档,并在StackOverflow上阅读了很多示例,但我仍然不确定如何使此操作正常工作 .

$api_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$service_url = http://api.remotehost.com/RemoteService.svc?wsdl

这是我试图访问的服务的WSDL,我试图访问GetVehicles()方法 . 当我使用以下方法创建新客户时:

$ client = new SoapClient($ service_url,array('cache_wsdl'=> 0));

我能够运行$ client - > __ getFunctions(),它正确地列出了所有服务的功能 . 但是,当我尝试使用以下方法访问GetVehicles方法时:

$vehicles=$client->GetVehicles($api_key);
var_dump($vehicles);

我收到“尝试执行未经授权的操作”错误 . 我不确定这是否意味着请求的形成不正确,或者我是否访问了错误的URL,或者确切地说是什么 . 我应该使用SoapClient的__soapCall或__doRequest方法来访问它吗?如果你看一下WSDL,你可以看到特定操作的其他动作URL,我应该在某个地方使用它们吗?

为了尝试和调试,我使用的是SoapUI程序 . 我输入WSDL URL,程序拉入功能列表,我可以从那里发出请求 . 当我使用GetVehicles提出请求时,我得到了正确的列表结果,所以我知道没有身份验证问题 .

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:api="http://api.remotehost.com">
   <soapenv:Header/>
   <soapenv:Body>
      <api:GetVehicles>
         <!--Optional:-->
         <api:APIKey>xxxxxxxxxxxxxxxxxxxxxxxx</api:APIKey>
      </api:GetVehicles>
   </soapenv:Body>
</soapenv:Envelope>

哪个会返回正确的车辆列表XML . 关于我做错了什么,我感到很困惑,而且我没时间完成这项工作 . 任何人都可以帮我指出正确的方向,让我知道我应该如何制作这个SOAP请求?任何帮助是极大的赞赏 . 谢谢!

2 回答

  • 2

    您需要指定如何使用 $api_key 值,如下所示:

    $client->GetVehicles(array('APIKey' => $api_key));
    

    要添加一些解释,请在此处致电:

    $client->GetVehicles($api_key);
    

    不告诉客户如何使用 $api_key . 如果查看 __getFunctions() 的输出,您会看到 GetVehicles 采用某种类型的参数结构:

    GetVehiclesResponse GetVehicles(GetVehicles $parameters)
    

    要查看该参数结构是什么,您必须发出 __getTypes() 调用 . 这是相关的一行:

    struct GetVehicles { string APIKey; }
    

    这意味着您想要传递的 GetVehicles 调用实际上是一个包含单个成员的结构 . 幸运的是PHP非常好,并且会接受一个匹配名称的数组 .

    调试此方法的有效方法是使用Fiddler作为您的呼叫的代理 . (如果你不在Windows上,你可以用Wireshark做类似的事情 . )加载Fiddler,然后像这样构建你的SoapClient:

    $opts = array('proxy_host' => 'localhost', 'proxy_port' => 8888);
    $client = new SoapClient($wsdl, $opts);
    

    然后,您通过客户端拨打的所有电话都将显示在Fiddler中供您查看 . 例如,您的原始电话在Fiddler中显示为:

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:ns1="http://api.silentpassenger.com">
        <SOAP-ENV:Body>
            <ns1:GetVehicles/>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    看到你的APIKey元素不存在可能会给你一个关于错误的有用线索 .

  • 2

    试试这个:

    $vehicles=$client->GetVehicles(array('APIKey' => $api_key));
    

相关问题