繁体   English   中英

SOAP XML response using cURL and PHP cisco CUCM API

[英]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;

所以它工作正常,但是当我执行 echo $response 时,我得到了完整的字符串响应,例如:

updatedtestApiapiaddUserTestapiaddUserTestapiaddUserTestapiUser....

而不是我在 var_dump($response) 时得到的 XML 字符串:

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

基本上我想要做的是访问我的 XML 值,如 PHP,我尝试使用 simplexml_load_string 我得到了一个空字符串和 SimpleXMLElement() 并且还得到了一个空的 ZA8CFDE6331BD59EB2AC966F8911C4。

我该如何使用它? 我做错了什么

您既需要内容类型 header 并接受 http 请求 header

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

如果没有Content-Type: application/xml ,Content-Type 将是multipart/form-data ,curl 将尝试将 JSON 放入 $_POST。
当您指定 application/xml 时,xml 将在请求正文中传递。
如果您应该将参数作为表单数据发送,那么您必须将 xml 转换为数组。

你不需要

 CURLOPT_CUSTOMREQUEST => 'POST'

只需使用

 CURLOPT_POST => true

如果您想在$response中查看响应 header ,请添加此内容。 查看响应 header 中的 Content-Type。

CURLOPT_HEADER => true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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