Monday, 8 July 2019

Why is my PHP WSDL is not sending a response (Zf2)

I used these two resources as launching pad for my creation of a WSDL endpoint server.

https://odan.github.io/2017/11/20/implementing-a-soap-api-with-php-7.html
https://www.youtube.com/watch?v=e_7jDqN2A-Y&t=799s

By combining these two I was able to come up with a hybrid system that works. My issue that I am trying resolve right now is getting a response back from the api.php/endpoint server.

In the odan git example, it worked to the letter. But once I made changes to the code that requires objects. I started getting errors.

PHP Notice:  Trying to get property of non-object

Here is a portion of the server code.


class wenoError
{
    public $response = "Sucess";

    public static function authenticate($header_params)
    {
        if($header_params->username == 'WEX' && $header_params->password == 'WEX1') return true;
        else throw new SOAPFault('Wrong user/pass combination', 601);
    }

    /**
    * @param string $payload
    * @return string $delivery
    */
    public function receivePayload($payload)
    {

        $xml = base64_decode($payload);

        $fileName = 'message-'.rand().'.xml'; 
        $file = file_put_contents('messages/'.$fileName, $xml);
        $xml2json = simplexml_load_string($xml);
        $jsonOut = json_encode($xml2json); 
        $arrayJson = json_decode($jsonOut, TRUE);
        //$seeArray = print_r($arrayJson, true);
        //file_put_contents('messages/converted-'.$fileName.'.json', $arrayJson['Header']['MessageID']);

        return $this->response;

    }

}
    $serverUrl = "https://localhost/apa/WenoErrors/api.php";
    $options = [
        'uri' => $serverUrl,
    ];
    $server = new Zend\Soap\Server('wsdl', $options);

    if (isset($_GET['wsdl'])) {
    $soapAutoDiscover = new \Zend\Soap\AutoDiscover(new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence());
    $soapAutoDiscover->setBindingStyle(array('style' => 'document'));
    $soapAutoDiscover->setOperationBodyStyle(array('use' => 'literal'));
    $soapAutoDiscover->setClass('wenoError');
    $soapAutoDiscover->setUri($serverUrl);

    header("Content-Type: text/xml");
    echo $soapAutoDiscover->generate()->toXml();
    } else {
    $soap = new \Zend\Soap\Server($serverUrl . '?wsdl');
    $soap->setObject(new \Zend\Soap\Server\DocumentLiteralWrapper(new wenoError()));
    $soap->handle();
    }


What I don't understand is the error message of $response being a non-object. According to the PHP manual https://www.php.net/manual/en/language.oop5.properties.php

The property is set correctly at the top of the class, the property is declared and a value us set.

What went wrong?



from Why is my PHP WSDL is not sending a response (Zf2)

No comments:

Post a Comment