Not Getting Correct Reponse in CURL Soap PHP

时间:2016-07-11 19:50:40

标签: php xml web-services curl soap

$length = strlen($requestXml); $ch = curl_init($serviceUrl);

curl_setopt($ch, CURLOPT_VERBOSE, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/xml; charset=utf-8',
    "SOAPAction: \"https://example.com/PMAPI/example/$action\"",
    "Content-length: ".strlen($requestXml)
));

curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

curl_setopt($ch, CURLOPT_COOKIEFILE, "cookieFIle.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookieFIle.txt");
curl_setopt($ch, CURLOPT_HEADER, 0);

$output = curl_exec($ch);
$info = curl_getinfo($ch);


curl_close($ch);

$xml=  simplexml_load_string($output);
print_r($xml);

Output I am Getting:

127464347

which is not in xml format. But in View Source The code is in XML format. But It didnt work when I am parsing this output string into xml parser.

output should be in following format which I can see in view-source page.

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><TopUpRequestResponse xmlns="https://dollarphone.com/PMAPI/PinManager"><TopUpRequestResult><responseCode>1</responseCode><TransId>27464347</TransId></TopUpRequestResult></TopUpRequestResponse></soap:Body></soap:Envelope>

When I am trying to access the object. The error is "Trying to get property of non-object".

Please Anyone help me solve this problem?

2 个答案:

答案 0 :(得分:1)

I found the solution: I just removed the tag from my result. and It works.

  $response1 = str_replace("<soap:Body>","",$output);
        $response2 = str_replace("</soap:Body>","",$response1);

        // converting to XML
        $parser = simplexml_load_string($response2);

Thank you

答案 1 :(得分:0)

Scratch everything I said before. It is working as it should

simplexml_load_string converts an XML string to an object. You're doing a print_r on a SimpleXMLElement object created by simplexml_load_string() and it's just printing out the fields in some human readable way.

Your object has a field with the value TransId of 27464347.

Try a var_dump($xml) instead of print_r and you'll see the object instead of some human-ish representation of it.

I'm unsure exactly, but just guessing the 1 preappended in the string you're seeing from you print_r might of it being the first object in the XML body? Here's a link to the __toString() method of the SimpleXMLElement http://php.net/manual/en/simplexmlelement.tostring.php