SOAP响应未以XML格式显示

时间:2018-06-01 06:58:02

标签: php xml api soap response

我是SOAP新手。我正在尝试创建一个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;
}
?>

functionalities.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 个答案:

答案 0 :(得分:1)

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

如果您要尝试做的是让浏览器将其识别为XML(就像告诉它一样;嘿,我知道您已经期待一个矩形,我发送给您一个,但特别是它也是一个正方形!&#34;),您可以通过在之前将任何内容打印到之前将此行添加到PHP中来添加标题

header("Content-type: text/xml");