dongyao1895 2018-02-14 09:18
浏览 48

PHP中的Web服务WCF使用cURL

I create web service WCF :

IService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace DLR
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
    [OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "SaveData/{resultData}", RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string SaveData(String resultData);
}
}

Service.cs

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Hosting;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
namespace DLR
{
    public class Service : DLR.IService
    {
        public string SaveData(String resultData) //save data to database
        {
            return "Josn is : " + resultData;
        }

    }

When using SoapClient, the result is returned without errors

But when used cURL not working, the result : cURL Error (22): The requested URL returned error: 415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'.

<?php
$data = '{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}' ;
$headers = array(' charset=utf-8','Accept: text/json','Cache-Control: no-cache','Pragma: no-cache');
$param = 'resultData' .json_encode($data) ;
$ch = curl_init('http://asdm.sa/Service.svc/SaveData');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER,1); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ) ; 
$info = curl_getinfo($ch);
print_r(array('the proble :', $info );
print_r("<hr>");
$result = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error
";
} else {
echo "Successful
";
}
curl_close($ch);

echo $result;

?>

web.config

<system.serviceModel>
    <bindings>
    <basicHttpBinding>
        <binding name="basicHttpBindingConfiguration">
            <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None" />
            </security>
        </binding>       
    </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
      <baseAddressPrefixFilters>
        <add prefix="http://asdm.sa/"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
    <services>
      <service name="DLR.Service" behaviorConfiguration="myServiceBehavior">
        <endpoint name="webHttpBinding" address="" listenUri="http://asdm.sa/Service.svc" binding="basicHttpBinding" contract="DLR.IService"   bindingConfiguration="basicHttpBindingConfiguration"/>
        <endpoint name="mexHttpBinding" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

How i sloved it

i used add wehttpbinding in system.serviceModel but same problem

can i use soap or wsdl to call web service in PHP by cURL

  • 写回答

1条回答 默认 最新

  • dongzhong1929 2018-02-16 06:31
    关注

    Just add

    "Content-Type: text/xml;charset=UTF-8;"
    

    to your cURL header request. That should allow you to progress further.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料