带有SuperPages API的PHP SoapClient

时间:2011-05-05 14:13:24

标签: php api soap soap-client

所以我对SOAP及其背后的全部想法都是全新的,但为了使用SuperPages API,我不得不弄明白。现在,我只是想绕过它,以及如何调用API。可以在此处找到API文档:http://advertising.superpages.com/spapiweb/v2。我可以通过标题登录,但我不知道从那里去哪里。这是我正在尝试运行的函数的文档:

getReportList

getReportList方法检索当前存储的报告列表。

请求:                                                                                                                                           

Field Name             Field Type  Field Description    Field
externalTransactionId   string(6)   External Transaction identifier used for logging. Provided by user. Optional

响应

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:getReportListResponse xmlns:ns2="webobjects.reporting.pbap.spmd.com">
     <return>
        <ns2:report>
           <ns2:reportName></ns2:reportName>
           <ns2:reportId></ns2:reportId>
           <ns2:reportType></ns2:reportType>
           <ns2:status></ns2:status>
           <ns2:startDate></ns2:startDate>
           <ns2:endDate></ns2:endDate>
           <ns2:createdDate></ns2:createdDate>
        </ns2:report>
        <ns2:totalRows></ns2:totalRows>
        <ns2:dateCreated></ns2:dateCreated>
        <ns2:internalTransactionId></ns2:internalTransactionId>
     </return>
  </ns2:getReportListResponse>
    </soap:Body>
</soap:Envelope>

我的代码:

 $options = array('trace' => true);
 $sp = new SoapClient('http://services.superpages.com/spexternalservicesv3/services/reportingservice?wsdl', $options);
 $header = new SoapHeader('[companyId]', '[username]', '[password]');
 $sp->__setSoapHeaders($header);

 echo "<pre>";
 var_dump($sp->__getFunctions()); //check that I'm at least doing something right
 echo "</pre>";

 $sp->__soapCall("getReportList",array("")); //empty array, because parameter is optional in documentation

我的结果:

array(5) {
  [0]=>
  string(59) "getReportURLResponse getReportURL(getReportURL $parameters)"
  [1]=>
  string(83) "scheduleAgencyReportResponse scheduleAgencyReport(scheduleAgencyReport $parameters)"
  [2]=>
  string(65) "scheduleReportResponse scheduleReport(scheduleReport $parameters)"
  [3]=>
  string(59) "deleteReportResponse deleteReport(deleteReport $parameters)"
  [4]=>
  string(62) "getReportListResponse getReportList(getReportList $parameters)"
}


Fatal error: Uncaught SoapFault exception: [soap:Client] Error reading XMLStreamReader. in /home/a2op/public_html/billing/sp/index.php:11 Stack trace: #0 /home/a2op/public_html/billing/sp/index.php(11): SoapClient->__soapCall('getReportList', Array) #1 {main} thrown in /home/a2op/public_html/billing/sp/index.php on line 11

我在这里做错了什么?


修改

通过

进行调试时
try {
    var_dump($sp->getReportList());
} catch (SoapFault $exception) {
    var_dump($exception->getMessage());
    var_dump($exception);
}

,这是错误转储

string(32) "Fault occurred while processing."
object(SoapFault)#5 (8) {
  ["message:protected"]=>
  string(32) "Fault occurred while processing."
  ["string:private"]=>
  string(0) ""
  ["code:protected"]=>
  int(0)
  ["file:protected"]=>
  string(43) "/home/a2op/public_html/billing/sp/index.php"
  ["line:protected"]=>
  int(19)
  ["trace:private"]=>
  array(2) {
    [0]=>
    array(6) {
      ["file"]=>
      string(43) "/home/a2op/public_html/billing/sp/index.php"
      ["line"]=>
      int(19)
      ["function"]=>
      string(6) "__call"
      ["class"]=>
      string(10) "SoapClient"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(2) {
        [0]=>
        string(13) "getReportList"
        [1]=>
        array(0) {
        }
      }
    }
    [1]=>
    array(6) {
      ["file"]=>
      string(43) "/home/a2op/public_html/billing/sp/index.php"
      ["line"]=>
      int(19)
      ["function"]=>
      string(13) "getReportList"
      ["class"]=>
      string(10) "SoapClient"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(0) {
      }
    }
  }
  ["faultstring"]=>
  string(32) "Fault occurred while processing."
  ["faultcode"]=>
  string(11) "soap:Server"
}

修改

运行后的XML输出

try{
    var_dump($sp->getReportList());
}catch (SoapFault $exception) {
    var_dump($exception->getMessage());
    var_dump($exception);
}
echo $sp->__getLastRequest();

-

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:ns1="webobjects.reporting.pbap.spmd.com">
  <SOAP-ENV:Header>
    <ns1:username>[username]</ns1:username>
    <ns1:password>[password]</ns1:password>
    <ns1:companyId>[companyId]</ns1:companyId>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:getReportList/>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

1 个答案:

答案 0 :(得分:0)

有趣:从未使用过它,但我认为这是错误的:

$header = new SoapHeader('[companyId]', '[username]', '[password]');
$sp->__setSoapHeaders($header);

我想你想要这个:

$headers = array();
$header[] = new SoapHeader('webobjects.reporting.pbap.spmd.com', 'username', '[username]');
$header[] = new SoapHeader('webobjects.reporting.pbap.spmd.com', 'password', '[password]');
$header[] = new SoapHeader('webobjects.reporting.pbap.spmd.com', 'companyId', '[companyId]');
$sp->__setSoapHeaders($headers);

请参阅http://www.php.net/manual/en/soapclient.setsoapheaders.php了解setSoapHeaders()多个标头调用,您应该转储请求XML以验证它看起来像http://advertising.superpages.com/spapiweb/v2?wicket:interface=:11:3处的示例:。

让我知道它是否有效。