如何使用 PHP 使用 WCF REST Web 服务的响应
我在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 WebGet 声明更改为以下内容:
然后,您只需调用 json_decode($result) 即可在 PHP 中使用响应。 (下面的示例)
如果将“json_decode”第二个参数设置为 true,您将获得关联数组而不是对象图。
Change the WebGet declaration to the following:
Then you can consume the response in PHP by just calling json_decode($result). (example below)
If you set the "json_decode" 2nd argument to true you'll get an associative array rather than an object graph.