代码之家  ›  专栏  ›  技术社区  ›  Roshni hegde

SOAP响应未以XML格式显示

  •  1
  • Roshni hegde  · 技术社区  · 8 年前

    我不熟悉肥皂。我试图创建一个SOAP API,将长url转换为短链接。

    这是我的密码

    在里面 shortlen_url。php (客户页面)

    require_once "nusoap/lib/nusoap.php";
    if(isset($_REQUEST['url']))
    {
        $longUrl= $_REQUEST['url'];
        $currentPath = $_SERVER['PHP_SELF']; 
        $pathInfo = pathinfo($currentPath);
        $hostName = $_SERVER['HTTP_HOST'];
        $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
        $client = new nusoap_client($protocol.$hostName.$pathInfo['dirname']."/functionalities.php?wsdl");
        $client->soap_defencoding = 'UTF-8';
        $client->decode_utf8 = false;
        $error = $client->getError();
        $doc = new DomDocument('1.0');
        $doc->preserveWhiteSpace = false;
        $doc->formatOutput = true;
    
        $root = $doc->createElement('root');
        $root = $doc->appendChild($root);
    
    
        $result = $client->call("getProd", array("longUrl" => $longUrl));
        $resultScheme = parse_url($result);
        if(isset($resultScheme['scheme']))
        {
            if($resultScheme['scheme'] == "http" || $resultScheme['scheme'] == "https")
            {
                $occ = $doc->createElement('message');
                $occ = $root->appendChild($occ);
                $child = $doc->createElement('shortURL');
                $child = $occ->appendChild($child);
                $value = $doc->createTextNode($result);
                $value = $child->appendChild($value);
    
                $child = $doc->createElement('Success');
                $child = $occ->appendChild($child);
                $value = $doc->createTextNode('A URL will be received by C4C which will be embedded in the SMS and sent to the customer');
                $value = $child->appendChild($value);
            }       
        }   
        else
        {
            $occ = $doc->createElement('error');
            $occ = $root->appendChild($occ);
            $child = $doc->createElement('errorMessage');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode($result);
            $value = $child->appendChild($value);
    
            $child = $doc->createElement('Failure');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode('A task will be created and assigned to the System Admin.');
            $value = $child->appendChild($value);
        }
        if ($client->fault)
        {
            $occ = $doc->createElement('error');
            $occ = $root->appendChild($occ);
    
            $child = $doc->createElement('errorMessage');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode($result);
            $value = $child->appendChild($value);
    
            $child = $doc->createElement('Failure');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode('A task will be created and assigned to the System Admin.');
            $value = $child->appendChild($value);
        }
    
        // get completed xml document
        $xml_string = $doc->saveXML() ;
        echo $xml_string;
    }
    ?>
    

    在里面 功能。php (服务器页面)

    <?php
    require_once "nusoap/lib/nusoap.php";
    
    function getProd($longUrl) {
        function get_bitly_short_url($longUrl,$login,$appkey,$format='txt') {
            $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.$longUrl.'&format='.$format;
            return curl_get_result($connectURL);
        }
    
        function curl_get_result($url) {
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
        }
    
        $short_url = get_bitly_short_url($longUrl,'x_xxxxxxxxxx','x_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','txt');
        return $short_url;
    }
    
    $server = new soap_server();
    $server->configureWSDL("ShortLink", "urn:ShortLink");
    $server->register("getProd",
        array("shortLink" => "xsd:string"),    
        array("return" => "xsd:string"),
        "urn:shortLink",
        "urn:shortLink#getProd",
        "rpc",
        "encoded",
        "Converting Long Url to Short Link");
    
    
    $server->service(file_get_contents("php://input"));
    
    ?>
    

    当我点击这个API时

    http://localhost/myproject/shorten_url.php?url=https://www.w3schools.com/php/showphp.asp?filename=demo_func_string_strpos
    

    它以字符串格式而不是xml格式显示正确的结果

    enter image description here

    当我点击ctrl+U时,它会显示如下视图代码

    enter image description here

    当我点击链接时,如何获得xml格式(而不是字符串)的响应。现在它的结果是字符串格式。请帮忙。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Bing    8 年前

    XML格式是一个字符串。有点像正方形是矩形,但不是所有的矩形都是正方形。JSON是另一种字符串格式。

    如果您试图让浏览器将其识别为XML(比如告诉它“嘿,我知道您期望的是一个矩形,我会给您发送一个,但具体来说,它也是一个正方形!”),你可以通过PHP添加一个标题 在你把任何东西打印到屏幕上之前 :

    header("Content-type: text/xml");
    
    推荐文章