修改通用 REST Helper PHP 示例代码以支持 XML DOM

发布于 2024-09-03 18:43:27 字数 1768 浏览 6 评论 0原文

我在 HTTP 中找到了这个示例 PHP 源代码来自 PHP 的 POST,没有 cURL

我需要一些帮助来修改示例 PHP 源以支持 XML DOM 来操作 REST API。

我认为如果我将下面的 XML 部分的 CASE 语句从 更新为 ,

$r = simplexml_load_string($res);

$r = new DOMDocument();
$r->load($res);

会起作用,但事实并非如此。 :(

任何帮助将不胜感激。

function rest_helper($url, $params = null, $verb = 'GET', $format = 'xml')
{
  $cparams = array(
    'http' => array(
      'method' => $verb,
      'ignore_errors' => true
    )
  );
  if ($params !== null) {
    $params = http_build_query($params);
    if ($verb == 'POST') {
      $cparams['http']['content'] = $params;
    } else {
      $url .= '?' . $params;
    }
  }

  $context = stream_context_create($cparams);
  $fp = fopen($url, 'rb', false, $context);
  if (!$fp) {
    $res = false;
  } else {
    // If you're trying to troubleshoot problems, try uncommenting the
    // next two lines; it will show you the HTTP response headers across
    // all the redirects:
    // $meta = stream_get_meta_data($fp);
    // var_dump($meta['wrapper_data']);
    $res = stream_get_contents($fp);
  }

  if ($res === false) {
    throw new Exception("$verb $url failed: $php_errormsg");
  }

  switch ($format) {
    case 'json':
      $r = json_decode($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as json");
      }
      return $r;

    case 'xml':
      $r = simplexml_load_string($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as xml");
      }
      return $r;
  }
  return $res;
}

I found this example PHP source code at HTTP POST from PHP, without cURL

I need some help modifying the example PHP source to support XML DOM for manipulating a REST API.

I thought that if I update the CASE statement for the XML section below from

$r = simplexml_load_string($res);

to

$r = new DOMDocument();
$r->load($res);

that it would work but it doesn't. :(

Any help would be appreciated.

function rest_helper($url, $params = null, $verb = 'GET', $format = 'xml')
{
  $cparams = array(
    'http' => array(
      'method' => $verb,
      'ignore_errors' => true
    )
  );
  if ($params !== null) {
    $params = http_build_query($params);
    if ($verb == 'POST') {
      $cparams['http']['content'] = $params;
    } else {
      $url .= '?' . $params;
    }
  }

  $context = stream_context_create($cparams);
  $fp = fopen($url, 'rb', false, $context);
  if (!$fp) {
    $res = false;
  } else {
    // If you're trying to troubleshoot problems, try uncommenting the
    // next two lines; it will show you the HTTP response headers across
    // all the redirects:
    // $meta = stream_get_meta_data($fp);
    // var_dump($meta['wrapper_data']);
    $res = stream_get_contents($fp);
  }

  if ($res === false) {
    throw new Exception("$verb $url failed: $php_errormsg");
  }

  switch ($format) {
    case 'json':
      $r = json_decode($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as json");
      }
      return $r;

    case 'xml':
      $r = simplexml_load_string($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as xml");
      }
      return $r;
  }
  return $res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-09-10 18:43:27

正确的形式是:

$r = new DOMDocument();
$r->loadXML($res);

请参阅 DOMDocument::loadXML 的文档DOMDocument::load() 方法从文件加载。

The correct form is:

$r = new DOMDocument();
$r->loadXML($res);

See the documentation for DOMDocument::loadXML The DOMDocument::load() method loads from a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文