locked
Call web service WCF in PHP use cURL RRS feed

  • Question

  • 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 IDLR
    {
        // 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]

            [WebGet(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;
            }
    
        }

    I call by PHP  cURL :

    <HTML>
    <HEAD>
     <TITLE>New Document</TITLE>
    </HEAD>
    <BODY>
    <?php
    $data = '{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}' ;
    $headers = array("Content-Type: text/xml;charset=UTF-8;");
    
    //$param = 'resultData' .json_encode($data) ;
    
    $ch = curl_init('http://asdm.sa/Service.svc/json/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);
    //;odata=verbose
    $info = curl_getinfo($ch);
    print_r( $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;
    
    ?>
    
    
    </BODY>
    </HTML>
    
    
    
    

    web.config :

           <system.serviceModel>
        <services>
          <service name="Service"  behaviorConfiguration="debug">
     
        <!-- these endpoint are necessary to return SOAP service -->
            <endpoint address=""
                         binding="basicHttpBinding"
                         contract="IDLR.IService" />
            <endpoint address="mex"
                      contract="IMetadataExchange" binding="mexHttpBinding"/>
             
            <!-- REST service return json -->
            <!--To call this endpoint use: [service].svc/json/[method_Name]-->
            <endpoint address="json"
                      binding="webHttpBinding" behaviorConfiguration="jsonBehavior"
                      contract="IDLR.IService" />
     
            <!-- REST service return xml -->
            <!--To call this endpoint use: [service].svc/xml/[method_Name]-->     
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
    		          <behavior name="debug">
              <serviceDebug includeExceptionDetailInFaults="true" />
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
     
          <endpointBehaviors>
              <behavior name="jsonBehavior">
                <enableWebScript/> <!-- use JSON serialization -->
              </behavior>
              <behavior name="xmlBehavior">
                <webHttp/> <!-- use XML serialization -->
              </behavior>
          </endpointBehaviors>
        </behaviors>
    		     <protocolMapping>
          <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>
    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
          </system.serviceModel>

    This is 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'

    how i sloved .

    ً،



    • Moved by Barry Wang Monday, February 19, 2018 2:18 AM
    Friday, February 16, 2018 1:54 PM

All replies

  • Hi aamoaa,

    Since the problem is more related to PHP and it is not related to UWP forum. UWP forumis where developers discuss writing Universal Windows Apps.

    I suggest that you might need to ask about this in the PHP forum.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 19, 2018 2:13 AM