Abdullah Al-Mohammadi
Abdullah Al-Mohammadi

Reputation: 15

web service WCF in PHP use 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\n";
} else {
echo "Successful\n";
}
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

Upvotes: 0

Views: 891

Answers (1)

cjd9
cjd9

Reputation: 59

Just add

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

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

Upvotes: 1

Related Questions