【问题标题】:Call REST WCF-Service creating by C# from PHP从 PHP 调用 C# 创建的 REST WCF-Service
【发布时间】:2017-07-24 12:51:14
【问题描述】:

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

【问题讨论】:

    标签: c# php web-services wcf web


    【解决方案1】:

    您需要为 wcf 服务定义 webHttpBinding 绑定,使其成为一个完整的 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

    【讨论】:

    • 感谢您对restfull wcf的澄清,但我需要通过程序PHP客户端使用这个RESTfull
    • 一旦它变得安静,您可以使用 curl 或类似方法通过 PHP 调用它,它将与您可以在 ajax 中使用的其他 url 类似。
    【解决方案2】:

    谢谢大家,这是我调用 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 中有参数时,我没有得到响应,在其他信息中,我使用邮递员进行测试

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2012-01-24
      • 2016-07-25
      • 1970-01-01
      相关资源
      最近更新 更多