在PHP中调用Web服务
i创建Web服务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;
}
}
使用SoapClient时,结果将在没有错误
的情况下返回但是,当使用卷曲不起作用时,结果是:卷曲错误(22(:请求的URL返回错误:415无法处理消息,因为内容类型"应用程序/x-www-form-urlCoded"不是预期类型'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_errorn";
} else {
echo "Successfuln";
}
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>
我如何滑过它
我在system.servicemodel中使用了添加wehttpbinding,但是相同的问题
我可以使用SOAP或WSDL通过Curl
只需添加
"Content-Type: text/xml;charset=UTF-8;"
符合您的卷曲标头请求。这应该使您进步。