从 PHP 调用 C# 创建的 REST WCF 服务



请我们帮我解决这个问题: 我正在用 REST 中的 C# 而不是 SOAP 创建一个 Web 服务 WCF,使用一些方法,如 https://..../ForMobile.svc/getUsersSMI/params,我想通过 PHP 调用这个 Web 服务,我的方法在输入中有一个参数。

您需要将 wcf 服务的 webHttpBinding 绑定定义为 restfull WCF 服务。 示例绑定如下

<system.serviceModel>
<services>
<service name="AndroidWCF.Service1" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<endpoint address="" binding="webHttpBinding"
behaviorConfiguration="webBehavior" contract="AndroidWCF.IService1">
<!--<identity>
<dns value="localhost"/>
</identity>-->
</endpoint>
<!--<endpoint address="mex"
binding="mexHttpBinding" contract="IMetadataExchange"/>-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>

您可以在此处找到完整的指南 https://www.codeproject.com/Tips/803009/Configure-WCF-Service-to-REST

感谢大家,这是我调用REST WCF的代码PHP:

$data = array(
'query1' => 'parameter1 ', 
'text' => 'Hi, Message from android example'
);
$url = "https://..../Mobile.svc/getParam";
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: 
application/json'));
$response = curl_exec ($ch);          
if(!$response){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
curl_close($ch);
echo html_entity_decode($response); 

这是我的代码PHP它的工作,但是当我的WCF中有参数时,我没有得到响应,在其他信息中,我使用邮递员进行测试

最新更新