PHP的肥皂-400错误的请求

时间:2018-06-25 17:47:19

标签: php soap

我试图使用一项服务,以使用Lebedev的Typograph打印文本。 我在localhost上使用了其示例中的代码,它可以工作,但是当我尝试在服务器上执行相同操作时,它将失败,并显示400代码。服务器在具有相同apache的相同本地计算机上运行。 我试图增加http.conf中的设置(我尝试使用非常短的文本): LimitRequestLine 16384 LimitRequestFieldSize 12288

我得到了以下答案:

""" quest\r\n Date: Mon, 25 Jun 2018 14:50:22 GMT\r\n Server: Apache\r\n Vary: Accept-Encoding\r\n Content-Length: 226\r\n Connection: close\r\n Content-Type: text/html; charset=iso-8859-1\r\n \r\n <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n <html><head>\n <title>400 Bad Request</title>\n </head><body>\n <h1>Bad Request</h1>\n <p>Your browser sent a request that this server could not understand.<br />\n """

班级代码:

class Typograph
{
    var $_entityType = 4;
    var $_useBr = 1;
    var $_useP = 1;
    var $_maxNobr = 3;
    var $_encoding = 'UTF-8';
    var $_quotA = 'laquo raquo';
    var $_quotB = 'bdquo ldquo';

    public function __construct ($encoding = 'utf-8')
    {
        $this->_encoding = $encoding;
    }

    public function htmlEntities()
    {
        $this->_entityType = 1;
    }

    public function xmlEntities()
    {
        $this->_entityType = 2;
    }

    public function mixedEntities()
    {
        $this->_entityType = 4;
    }

    public function noEntities()
    {
        $this->_entityType = 3;
    }

    public function br ($value)
    {
        $this->_useBr = $value ? 1 : 0;
    }

    public function p ($value)
    {
        $this->_useP = $value ? 1 : 0;
    }

    public function nobr ($value)
    {
        $this->_maxNobr = $value ? $value : 0;
    }

    public function quotA ($value)
    {
        $this->_quotA = $value;
    }

    public function quotB ($value)
    {
        $this->_quotB = $value;
    }

    public function processText ($text)
    {
        $text = str_replace ('&', '&amp;', $text);
        $text = str_replace ('<', '&lt;', $text);
        $text = str_replace ('>', '&gt;', $text);

        $SOAPBody = '<?xml version="1.0" encoding="' . $this->_encoding . '"?>
        <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>
            <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">
              <text>' . $text . '</text>
              <entityType>' . $this->_entityType . '</entityType>
              <useBr>' . $this->_useBr . '</useBr>
              <useP>' . $this->_useP . '</useP>
              <maxNobr>' . $this->_maxNobr . '</maxNobr>
              <quotA>' . $this->_quotA . '</quotA>
              <quotB>' . $this->_quotB . '</quotB>
            </ProcessText>
          </soap:Body>
        </soap:Envelope>';

        $host = 'typograf.artlebedev.ru';
        $SOAPRequest = 'POST /webservices/typograf.asmx HTTP/1.1
                        Host: typograf.artlebedev.ru
                        Content-Type: text/xml
                        Content-Length: ' . strlen ($SOAPBody). '
                        SOAPAction: "http://typograf.artlebedev.ru/webservices/ProcessText"
                        '.
            $SOAPBody;

        $remoteTypograf = fsockopen ($host, 80);
        fwrite ($remoteTypograf, $SOAPRequest);
        $typografResponse = '';

        while (!feof ($remoteTypograf))
        {
            $typografResponse .= fread ($remoteTypograf, 8192);
        }
        fclose ($remoteTypograf);


        $startsAt = strpos ($typografResponse, '<ProcessTextResult>') + 19;
        $endsAt = strpos ($typografResponse, '</ProcessTextResult>');
        $typografResponse = substr ($typografResponse, $startsAt, $endsAt - $startsAt - 1);

        $typografResponse = str_replace ('&amp;', '&', $typografResponse);
        $typografResponse = str_replace ('&lt;', '<', $typografResponse);
        $typografResponse = str_replace ('&gt;', '>', $typografResponse);


        return  $typografResponse;
    }
}

我的电话很简单:

$typograph = new App\services\Typograph();
return $typograph->processText('any text');

我尝试了GET和POST方法,并且也使用phpstorm发送了Ajax请求。

你能帮我吗?我找不到与我的问题有关的东西。我什至不明白什么是错。为什么它可以在localhost上运行,而不能在同一服务器上的site.dev上运行。

1 个答案:

答案 0 :(得分:0)

解决方案很简单:我只需要删除空格即可。

$SOAPRequest = 'POST /webservices/typograf.asmx HTTP/1.1' . "\n" .
                    'Host: typograf.artlebedev.ru'  . "\n" .
                    'Content-Type: text/xml'  . "\n" .
                    'Content-Length: ' . strlen ($SOAPBody)  . "\n" .
                    'SOAPAction: "http://typograf.artlebedev.ru/webservices/ProcessText"'  . "\n\n" .
        $SOAPBody;