2
一個WCF REST Web服務的響應我都內置了NET Web服務,看起來是這樣的:如何消耗從PHP
[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服務響應?
可能更容易使用這個害蟲。在您更改API以提供JSON內容後,只需使用PestJSON而不是PestXML。 –