web service WCF in PHP use cURL

Feb 14 2018 3:51 AM

Hi, 

I create web service WCF :

IService.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. namespace DLR  
  9. {  
  10. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.  
  11. [ServiceContract]  
  12. public interface IService  
  13. {  
  14.     [OperationContract]  
  15. [WebInvoke(Method = "POST", UriTemplate = "SaveData/{resultData}", RequestFormat = WebMessageFormat.Json,  
  16.         ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest)]  
  17.     string SaveData(String resultData);  
  18. }  
  19. }  
 Service.cs
  1. using Newtonsoft.Json;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Configuration;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7. using System.IO;  
  8. using System.Linq;  
  9. using System.Runtime.Serialization;  
  10. using System.ServiceModel;  
  11. using System.Text;  
  12. using System.Web.Hosting;  
  13.   
  14. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.  
  15. namespace DLR  
  16. {  
  17.     public class Service : DLR.IService  
  18.     {  
  19.         public string SaveData(String resultData) //save data to database  
  20.         {  
  21.             return "Josn is : " + resultData;  
  22.         }  
  23.   
  24.     }  
 
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'.

  1. <?php  
  2. $data = '{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}' ;  
  3. $headers = array(' charset=utf-8','Accept: text/json','Cache-Control: no-cache','Pragma: no-cache');  
  4. $param = 'resultData' .json_encode($data) ;  
  5. $ch = curl_init('http://asdm.sa/Service.svc/SaveData');  
  6. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  7. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  8. curl_setopt($ch, CURLOPT_HEADER,1);   
  9. curl_setopt($ch, CURLOPT_FAILONERROR, 1);  
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
  11. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  12. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);  
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  14. curl_setopt($ch, CURLOPT_TIMEOUT, 50);  
  15. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ) ;   
  16. $info = curl_getinfo($ch);  
  17. print_r(array('the proble :'$info );  
  18. print_r("<hr>");  
  19. $result = curl_exec($ch);  
  20. $curl_errno = curl_errno($ch);  
  21. $curl_error = curl_error($ch);  
  22. if($curl_errno > 0) {  
  23. echo "cURL Error ($curl_errno): $curl_error\n";  
  24. else {  
  25. echo "Successful\n";  
  26. }  
  27. curl_close($ch);  
  28.   
  29. echo $result;  
  30.   
  31. ?>  
web.config
  1. <system.serviceModel>  
  2.     <bindings>  
  3.     <basicHttpBinding>  
  4.         <binding name="basicHttpBindingConfiguration">  
  5.             <security mode="None">  
  6.                 <transport clientCredentialType="None" proxyCredentialType="None" />  
  7.             </security>  
  8.         </binding>         
  9.     </basicHttpBinding>  
  10.     </bindings>  
  11.     <behaviors>  
  12.       <serviceBehaviors>  
  13.         <behavior name="myServiceBehavior">  
  14.           <serviceMetadata httpGetEnabled="true"/>  
  15.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  16.         </behavior>  
  17.         <behavior>  
  18.           <serviceMetadata httpGetEnabled="false"/>  
  19.           <serviceDebug includeExceptionDetailInFaults="true"/>  
  20.         </behavior>  
  21.       </serviceBehaviors>  
  22.       <endpointBehaviors>  
  23.         <behavior name="webHttp">  
  24.           <webHttp/>  
  25.         </behavior>  
  26.       </endpointBehaviors>  
  27.     </behaviors>  
  28.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true">  
  29.       <baseAddressPrefixFilters>  
  30.         <add prefix="http://asdm.sa/"/>  
  31.       </baseAddressPrefixFilters>  
  32.     </serviceHostingEnvironment>  
  33.     <services>  
  34.       <service name="DLR.Service" behaviorConfiguration="myServiceBehavior">  
  35.         <endpoint name="webHttpBinding" address="" listenUri="http://asdm.sa/Service.svc" binding="basicHttpBinding" contract="DLR.IService"   bindingConfiguration="basicHttpBindingConfiguration"/>  
  36.         <endpoint name="mexHttpBinding" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
  37.       </service>  
  38.     </services>  
  39.   </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


Answers (6)