Roshni hegde
Roshni hegde

Reputation: 415

SOAP response is not displaying in XML Format

I am new to SOAP. I am trying to create a SOAP API which converts long url to short link.

Here is my code

In shortlen_url.php (Client Page)

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;
}
?>

In functionalities.php (Server Page)

<?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"));

?>

When I hit this API

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

It displays proper result but in string format not in xml format

enter image description here

when i click on ctrl+U it shows view code like this

enter image description here

How can I get response in xml format(not in string) when I hit link. Now it results in string format. Please Help.

Upvotes: 1

Views: 741

Answers (1)

Bing
Bing

Reputation: 3171

XML format is a string. Sort of like how a square is a rectangle, but not all rectangles are squares. JSON is another string format.

If what you're trying to do is get the browser to recognize it as XML (like telling it "hey, I know you're expecting a rectangle and I'm sending you one, but specifically it's also a square!"), you can add a header via PHP by putting this line before you print anything to the screen:

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

Upvotes: 1

Related Questions