简体   繁体   中英

SOAP XML response using cURL and PHP cisco CUCM API

I'm trying to pull data from a SOAP API (Call manager CISCO) using CURL in PHP with POSTMAN tool like this:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'url',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">
    <soapenv:Header/>
    <soapenv:Body>
        <ns:getUser sequence="?">
            <userid>apiUser</userid>
        </ns:getUser>
    </soapenv:Body>
</soapenv:Envelope>',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic ..',
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

So it is working fine but i get a full string response when i do echo $response like:

updatedtestApiapiaddUserTestapiaddUserTestapiaddUserTestapiUser....

Instead of the XML string that i got when i var_dump($response):

'<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:getUserResp.....

Basically what i want to do is to access my XML values like in PHP, i tried using simplexml_load_string i got an empty string and also SimpleXMLElement() and also got an empty object..

How can i work with it? what i am doing wrong

You need both content-type header and accept http request header

$request[] = 'Content-Type: application/xml';
$request[] = 'Accept: application/xml';
$request[] = 'Authorization: Basic';
CURLOPT_HTTPHEADER => $request

With out the Content-Type: application/xml the Content-Type will be multipart/form-data and curl will attempt to put the JSON in the $_POST.
When you specify application/xml the xml will be passed in the request body.
If you are supposed to send the parameters as form data then you must convert the xml to an array.

You do not need

 CURLOPT_CUSTOMREQUEST => 'POST'

Just use

 CURLOPT_POST => true

If you want to see the response header in your $response , add this. Look at the Content-Type in the response header.

CURLOPT_HEADER => true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM