A
A
Andrew2015-07-09 17:51:59
PHP
Andrew, 2015-07-09 17:51:59

SOAP error looks like we got no XML document. How to fix?

Good afternoon.
I started to deal with SOAP, sketched out a simple news service and a client for it and got an error.
What is:
- news service, located in the news folder (all this is running on OpenServre`e)
- soap-server, located in the news / soap folder
- wsdl file news.wsdl, located in the news / soap folder
- soap-client, located in the localhost folder
What I do: I
start OpenServer, go to localhost - crashes "Client operation returned an error: looks like we got no XML document".
I checked the encodings and BOM characters, everything is in order.
Sources:
- soap-server.php

<?php
require "../class/NewsDB.class.php";
class NewsService extends NewsDB{
  /* Метод возвращает новость по её идентификатору */
  function getNewsById($id){
    try{
      $sql = "SELECT id, title, 
          (SELECT name FROM category WHERE category.id=msgs.category) as category, description, source, datetime 
          FROM msgs
          WHERE id = $id";
      $result = $this->_db->query($sql);
      if (!is_object($result)) 
        throw new Exception($this->_db->lastErrorMsg());
      return base64_encode(serialize($this->sqlToArray($result)));
    }catch(Exception $e){
      throw new SoapFault('getNewsById', $e->getMessage());
    }
  }
  /* Метод считает количество всех новостей */
  function getNewsCount(){
    try{
      $sql = "SELECT count(*) FROM msgs";
      $result = $this->_db->querySingle($sql);
      if (!$result) 
        throw new Exception($this->_db->lastErrorMsg());
      return $result;
    }catch(Exception $e){
      throw new SoapFault('getNewsCount', $e->getMessage());
    }
  }
  /* Метод считает количество новостей в указанной категории */
  function getNewsCountByCat($cat_id){
    try{
      $sql = "SELECT count(*) FROM msgs WHERE category=$cat_id";
      $result = $this->_db->querySingle($sql);
      if (!$result) 
        throw new Exception($this->_db->lastErrorMsg());
      return $result;
    }catch(Exception $e){
      throw new SoapFault('getNewsCountByCat', $e->getMessage());
    }
  }
}
// Отключение кеширования wsdl-документа
ini_set("soap.wsdl_cache_enabled", "0");
// Создание SOAP-сервера
$server = new SoapServer("http://news/soap/news.wsdl");
// Регистрация класса
$server->setClass("NewsService");
// Запуск сервера
$server->handle();

-soap-client.php
<?php
$client = new SoapClient("http://news/soap/news.wsdl");
try{
// Сколько новостей всего?
  $result = $client->getNewsCount();
  echo "<p>Всего новостей: $result</p>";
// Сколько новостей в категории Политика?
  $result = $client->getNewsCountByCat(1);
  echo "<p>Всего новостей в категории Политика:$result</p>";
// Покажем конкретную новость
  $result = $client->getNewsById(5);
  $news = unserialize(base64_decode($result));
  var_dump($news);
}catch(SoapFault $e){
  echo 'Операция '.$e->faultcode.' вернула ошибку: '.$e->getMessage();
}

-news.wsdl
<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='News' 
    targetNamespace='http://news'
    xmlns:tns=' http://news'
    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
    xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
    xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
    xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='getNewsByIdRequest'> 
    <part name='id' type='xsd:integer'/> 
  </message> 
  <message name='getNewsByIdResponse'> 
    <part name='item' type='xsd:base64Binary'/> 
  </message> 

  <message name='getNewsCountResponse'> 
    <part name='count' type='xsd:integer'/> 
  </message>

  <message name='getNewsCountByCatRequest'> 
    <part name='cat_id' type='xsd:integer'/> 
  </message> 
  <message name='getNewsCountByCatResponse'> 
    <part name='count' type='xsd:integer'/> 
  </message>


  <portType name='NewsPortType'> 
    <operation name='getNewsById'> 
      <input message='tns:getNewsByIdRequest'/> 
      <output message='tns:getNewsByIdResponse'/> 
    </operation>
    <operation name='getNewsCount'> 
      <output message='tns:getNewsCountResponse'/> 
    </operation>	
    <operation name='getNewsCountByCat'> 
      <input message='tns:getNewsCountByCatRequest'/> 
      <output message='tns:getNewsCountByCatResponse'/> 
    </operation>	
  </portType>

  <binding name='NewsBinding' type='tns:NewsPortType'> 
    <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='getNewsById' /> 
    <operation name='getNewsCount' />
    <operation name='getNewsCountByCat' />  
  </binding> 

  <service name='NewsService'> 
    <port name='NewsPort' binding='NewsBinding'> 
      <soap:address location='http://news/soap/soap-server.php'/>
    </port> 
  </service> 
</definitions>

I would appreciate any ideas and your time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leonid, 2016-10-18
@Lukich87

It's all about the paths.
I checked your code, specifying my directories - everything works.
ps An example from the course "Specialist".

J
Johnny Show, 2017-08-27
@X-core

I went through this lesson ...
I also encountered such a problem.
made all the fixes that the author of the video talked about, but he did not say that you need to change access to $_db from private to protected, because the class property is not visible in the soap-server.php file.
Answer to the question: write in the server and client files: $server = new SoapServer($_SERVER['HTTP_REFERER']."news.wsdl");
$client = new SoapClient($_SERVER['HTTP_REFERER'] . "soap/news.wsdl");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question