如何使用 PHP 使用 WCF REST Web 服务的响应

发布于 2024-12-01 20:56:04 字数 1642 浏览 0 评论 0原文

我在 .net 中内置了 Web 服务,看起来像这样:

[ServiceContract]
public interface IRestService
{
    [OperationContract]
    [WebGet(UriTemplate = "object/{name}")]
    Object GetObject(string name);
}

public class api : IRestService
{
    OSAE.OSAE osae = new OSAE.OSAE("WebService");

    public Object GetObject(string name)
    {
        // lookup object 
        OSAEObject OSAEobj = osae.GetObjectByName(name);
        Object obj = new Object();
        obj.Name = OSAEobj.Name;
        obj.Address = OSAEobj.Address;
        obj.Type = OSAEobj.Type;
        obj.Container = OSAEobj.Container;
        obj.Enabled = OSAEobj.Enabled;
        obj.Description = OSAEobj.Description;

        return obj;
    }
 }

当我只使用浏览器调用它时,这将给出如下所示的响应:

<Object xmlns="http://schemas.datacontract.org/2004/07/OSAERest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Address/>
<Container>SYSTEM</Container>
<Description>Email</Description>
<Enabled>0</Enabled>
<Name>Email</Name>
<Properties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true"/>
<Type>EMAIL</Type>
</Object>

我需要能够使用 PHP 来使用它。我尝试过使用 Pest (https://github.com/educoder/pest),但我无法让任何东西发挥作用。这是我的尝试:

<?php
require_once 'Includes/PestXML.php';
$pest = new PestXML('http://localhost:8732/api');
$things = $pest->get('/object/email');
$names = $things->xpath('//Object/Description');
while(list( , $node) = each($names)) {
    echo $node,"\n";
}
?>

如何使用 PHP 正确使用我的 Web 服务响应?

I have webservice built in .net that looks something like this:

[ServiceContract]
public interface IRestService
{
    [OperationContract]
    [WebGet(UriTemplate = "object/{name}")]
    Object GetObject(string name);
}

public class api : IRestService
{
    OSAE.OSAE osae = new OSAE.OSAE("WebService");

    public Object GetObject(string name)
    {
        // lookup object 
        OSAEObject OSAEobj = osae.GetObjectByName(name);
        Object obj = new Object();
        obj.Name = OSAEobj.Name;
        obj.Address = OSAEobj.Address;
        obj.Type = OSAEobj.Type;
        obj.Container = OSAEobj.Container;
        obj.Enabled = OSAEobj.Enabled;
        obj.Description = OSAEobj.Description;

        return obj;
    }
 }

This will give a response that looks like this when I just use a browser to call it:

<Object xmlns="http://schemas.datacontract.org/2004/07/OSAERest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Address/>
<Container>SYSTEM</Container>
<Description>Email</Description>
<Enabled>0</Enabled>
<Name>Email</Name>
<Properties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true"/>
<Type>EMAIL</Type>
</Object>

I need to be able to consume this with PHP. I have tried using Pest (https://github.com/educoder/pest), but I can't get anything to work. Here is my attempt:

<?php
require_once 'Includes/PestXML.php';
$pest = new PestXML('http://localhost:8732/api');
$things = $pest->get('/object/email');
$names = $things->xpath('//Object/Description');
while(list( , $node) = each($names)) {
    echo $node,"\n";
}
?>

How can I properly consume my web service responses with PHP?

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

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

发布评论

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

评论(1

回眸一遍 2024-12-08 20:56:04

将 WebGet 声明更改为以下内容:

[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "object/{name}")]

然后,您只需调用 json_decode($result) 即可在 PHP 中使用响应。 (下面的示例)

$url = "http://localhost:8732/api/object/wibble";
$response = file_get_contents($url);

$jsonData = json_decode($response);

var_dump($jsonData);

如果将“json_decode”第二个参数设置为 true,您将获得关联数组而不是对象图。

Change the WebGet declaration to the following:

[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "object/{name}")]

Then you can consume the response in PHP by just calling json_decode($result). (example below)

$url = "http://localhost:8732/api/object/wibble";
$response = file_get_contents($url);

$jsonData = json_decode($response);

var_dump($jsonData);

If you set the "json_decode" 2nd argument to true you'll get an associative array rather than an object graph.

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