PHP 5.4中的SOAP响应为NULL,但在PHP 5.2中有效

时间:2014-01-15 22:11:05

标签: php soap wsdl

这个类在PHP 5.2中很有用,但在PHP 5.4中,私有函数callMethod中的__doRequest方法返回NULL。

因此,脚本的输出是“NULL SOAP RESPONSE”

请参阅下面的完整课程

<?php
class ipm
{
    //BEGIN VARIABLES
    public $soapVersion;
    public $soapLocation;
    public $soapContext;
    public $soapParameters;
    public $soapClient;
    public $soapCookies;
    public $soapService;

    const groupID = '104362395629'; //MAIN GROUP NUMBER WITHIN IPM, NEVER CHANGES
    //END VARIABLES

    //BEGIN CONSTRUCTOR
    function __construct($soapService)
    {
            $this->soapService = $soapService; //STATUS OR CONFIG
            $this->soapVersion = '1.2';
            $this->soapLocation = 'https://webservice.domain/soap/'.$this->soapService.'.asmx';
            $this->soapContext = stream_context_create(array('ssl' => array('verify_peer' => TRUE)));
            $this->soapParameters = array (
                                            'location' => $this->soapLocation,
                                            'login' => 'user',
                                            'password' => 'password',
                                            'stream_context' => $this->soapContext,
                                            'trace' => 1,
                                            'keep_alive' => 1,
                                            'cache_wsdl' => WSDL_CACHE_NONE
                                            );
            $this->soapClient = new SoapClient('./'.$this->soapService.'.wsdl',$this->soapParameters);
    }
    //END CONSTRUCTOR

    //BEGIN PRIVATE FUNCTIONS
    private function schemaOpen($fn='',$vars)
    {
            $tmp = '<?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/">';

            if(empty($vars))
            {
            return $tmp.'<soap:Body><'.$fn.' xmlns="'.$this->schemaBase().'" />';
            }
            else
            {
            return $tmp.'<soap:Body><'.$fn.' xmlns="'.$this->schemaBase().'">';
            }

    }
    private function schemaClose($fn='',$vars)
    {
            if(empty($vars))
            {
            return '</soap:Body></soap:Envelope>';
            }
            else
            {
            return '</'.$fn.'></soap:Body></soap:Envelope>';
            }

    }
    private function schemaBase($fn='')
    {
            return 'http://schemas.ipmonitor.com/ipm70/'.$fn;
    }
    private function dumpDOMnodeList($xml)
    {
            $docTmp = new DOMDocument();
            foreach($xml as $n) $docTmp->appendChild($docTmp->importNode($n,true));
            echo '<pre>';
            print_r(htmlentities($docTmp->saveHTML()));
            echo '</pre>';
    }
    private function debugSoap()
    {
            echo '<pre>';
            echo $this->soapClient->__getLastRequestHeaders();
            echo $this->soapClient->__getLastRequest();
            echo $this->soapClient->__getLastResponseHeaders();
            echo '</pre>';
    }
    private function soapRequest($fn='',$vars=array())
    {
            $xml=$this->schemaOpen($fn,$vars);
            foreach($vars as $key=>$val)
            {
                    $xml .= '<'.$key.'>'.$val.'</'.$key.'>';
            }
            $xml.=$this->schemaClose($fn,$vars);

            return $xml;
    }
    private function callMethod($fn,$vars=array())
    {
            $soapRequest = $this->soapRequest($fn,$vars);
            $soapAction  = $this->schemaBase($fn);

            $soapResponse =  $this->soapClient->__doRequest($soapRequest, $this->soapLocation, $soapAction, $this->soapVersion,0);
            $this->logSoap($soapRequest, $this->soapLocation, $soapAction, $this->soapVersion, $soapResponse,$this->debugSoap());

            return $soapResponse;

    }
    private function logSoap($request, $location, $action, $version, $reponse, $debug)
    {
            file_put_contents('soap_log', date('m-d-Y H:i:s')."\r\nRequest: $request\r\n\r\nLocation: $location\r\nAction: $action\r\nVersion: $version\r\nResponse: $response\r\nDebug: $debug\r\n\r\n");
    }
    //END PRIVATE FUNCTIONS

    //BEGIN METHODS
    public function GetGroup($rtGroupID)
    {
            /*
            GetGroup returns the status and some minor details about a specific Monitor.
            */

            return $this->callMethod(__FUNCTION__,array('id'=>$rtGroupID));
    }
    public function GetGroups()
    {
            /*
            GetGroups returns the status of members and depends of a specific Group
            */

            return $this->callMethod(__FUNCTION__,array());
    }
    public function GetMonitors($rtGroupID)
    {
            /*
            GetMonitor returns the status of the members and depends of a specific Group.
            */

            return $this->callMethod(__FUNCTION__,array('groupid'=>$rtGroupID,'bTroubleOnly'=>'TRUE'));
    }
    public function GetMonitor($rtMonitorID)
    {
            /*
            GetMonitor returns the status and some minor details about a specific Monitor.
            */

            return $this->callMethod(__FUNCTION__,array('id'=>$rtMonitorID));
    }
    public function MonitorView($rtMonitorID)
    {
            /*
            MonitorView retrieves an ipMonitor 7 Monitor objects's settings in xml format.
            */

            return $this->callMethod(__FUNCTION__,array('mon_id'=>$rtMonitorID));
    }
    public function GroupList()
    {
            /*
            GroupList retrieves the ID's of all the existing Group objects.
            */

            return $this->callMethod(__FUNCTION__,array());
    }
    public function GroupView($rtGroupID)
    {
            /*
            GroupView retrieves an ipMonitor 7 Group objects's settings in xml format.
            */

            return $this->callMethod(__FUNCTION__,array('groupid'=>$rtGroupID));
    }
    //END METHODS
}
/*  BEGIN GET ALL DOWN MONITORS  */
$ipm            = new ipm('status');
$ipmGroups      = $ipm->GetGroups();
if(!empty($ipmGroups))
{
    $doc = new DOMDocument();
    $doc->LoadXML($ipmGroups);
    $xpath = new DOMXpath($doc);
    $xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
    $xpath->registerNamespace('def', 'http://schemas.ipmonitor.com/ipm70/');
    $ipmGroupsAlarming = $xpath->query("/soap:Envelope/soap:Body/def:GetGroupsResponse/def:GetGroupsResult/def:rtGroup[(contains(def:status,'lost') or contains(def:status,'warn') or contains(def:status,'down')) and contains(def:isroot,'false')]");

    $numGroupsAlarming = $ipmGroupsAlarming->length;
    if($numGroupsAlarming === 0)
    {
            echo 'No issues'."\r\n";
    }
    else
    {
            unset($ipm,$ipmGroups,$doc,$xpath,$numGroupsAlarming);
            foreach($ipmGroupsAlarming as $ipmGroupAlarming)
            {
                    $groupName              = $ipmGroupAlarming->getElementsByTagName('name')->item(0)->nodeValue;
                    $groupStatus    = $ipmGroupAlarming->getElementsByTagName('status')->item(0)->nodeValue;
                    $groupID                = $ipmGroupAlarming->getElementsByTagName('id')->item(0)->nodeValue;

                    echo $groupName.'<br/>'."\r\n";

                    $ipm            = new ipm('status');
                    $ipmMonitors= $ipm->GetMonitors($groupID);

                    $doc = new DOMDocument();
                    $doc->LoadXML($ipmMonitors);
                    $xpath = new DOMXpath($doc);
                    $xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
                    $xpath->registerNamespace('def', 'http://schemas.ipmonitor.com/ipm70/');
                    $ipmMonitorsAlarming = $xpath->query("/soap:Envelope/soap:Body/def:GetMonitorsResponse/def:GetMonitorsResult/def:rtMonitor");

                    $numMonitorsAlarming = $ipmMonitorsAlarming->length;
                    if($numMonitorsAlarming === 0)
                    {
                            echo 'No Monitors Down Under This Device<br/>'."\r\n";
                    }
                    else
                    {
                            foreach($ipmMonitorsAlarming as $ipmMonitorAlarming)
                            {
                                    $monitorName    = $ipmMonitorAlarming->getElementsByTagName('name')->item(0)->nodeValue;
                                    $monitorStatus  = $ipmMonitorAlarming->getElementsByTagName('status')->item(0)->nodeValue;
                                    $monitorID              = $ipmMonitorAlarming->getElementsByTagName('id')->item(0)->nodeValue;

                                    echo '--&gt; '.$monitorStatus.' - '.$monitorName.'<br/>'."\r\n";
                            }
                    }
                    unset($ipm,$ipmMonitors,$doc,$xpath,$numMonitorsAlarming,$ipmMonitorsAlarming);
                    echo '<br/>';
            }
    }
}
elseif($ipmGroups == NULL) {echo 'NULL SOAP RESPONSE';}
else
{
echo 'No issues'."\r\n";
}
/*  END GET ALL DOWN MONITORS  */
?>

新信息:

PHP 5.4 debug:

    Request: <?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><GetGroups xmlns="http://schemas.ipmonitor.com/ipm70/" /></soap:Body>
    </soap:Envelope>

    Location: https://webservice.domain/soap/status.asmx
    Action: http://schemas.ipmonitor.com/ipm70/GetGroups
    Version: 1.2
    Response:
    Debug: POST /soap/status.asmx HTTP/1.1
    Host: webservice.domain
    Connection: Keep-Alive
    User-Agent: PHP-SOAP/5.4.17
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "http://schemas.ipmonitor.com/ipm70/GetGroups"
    Content-Length: 303
    Authorization: Basic blah blah blah

PHP 5.2 debug:

    Request: <?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><GetGroups xmlns="http://schemas.ipmonitor.com/ipm70/" /></soap:Body>
    </soap:Envelope>

    Location: https://webservice.domain/soap/status.asmx
    Action: http://schemas.ipmonitor.com/ipm70/GetGroups
    Version: 1.2
    Response: <?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>...</soap:Body></soap:Envelope>
    Debug: POST /soap/status.asmx HTTP/1.1
    Host: webservice.domain
    Connection: Keep-Alive
    User-Agent: PHP-SOAP/5.2.10
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "http://schemas.ipmonitor.com/ipm70/GetGroups"
    Content-Length: 303
    Authorization: Digest username="usename", realm="ipMonitor@ipmonitor.com" ...

    HTTP/1.0 200 ok
    Server: ipMonitor 10.6
    Content-Type: text/xml; charset=utf-8
    Date: Thu, 16 Jan 2014 12:31:23 GMT
    Connection: Keep-Alive
    Content-Length: 99492

如您所见,响应为空,授权行是Basic vs Digest但代码是否相同?

如果我尝试设置“'authentication'=&gt; SOAP_AUTHENTICATION_DIGEST”,那么响应一起没有授权行。

0 个答案:

没有答案