获得空值. [英] Getting Null Value.

查看:65
本文介绍了获得空值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了WCF服务以从php应用程序添加订单数据.当我从php curl发布json数据时,它总是保存Null值.

I have created WCF service to add orders data from php application. When i am posting json data from php curl it always save Null value.

WCF语法

--------------------------------------------------- ----------------------

---------------------------------------------------------------------

[ServiceContract]
   公共接口IOrderApi
    {
        [OperationContract]
        [WebInvoke(Method ="GET",
                    RequestFormat = WebMessageFormat.Json,
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate =" Orders/"))]
       字符串GetOrder();

        [OperationContract]
        [WebInvoke(Method ="POST",
                    RequestFormat = WebMessageFormat.Json,
                    ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare,UriTemplate ="AddOrders/")]
        void AddOrder(Order _orders);

    }

[ServiceContract]
    public interface IOrderApi
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
                    RequestFormat = WebMessageFormat.Json,
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "Orders/")]
        string GetOrder();

        [OperationContract]
        [WebInvoke(Method = "POST",
                    RequestFormat = WebMessageFormat.Json,
                    ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "AddOrders/")]
        void AddOrder(Order _orders);

    }

--------------------------------------------------- --------------

-------------------------------------------------------------

OrderApi.svc.cs

OrderApi.svc.cs

公共类OrderApi:IOrderApi
    {
       静态IOrderRepository repo = new OrderRepository();

       公共字符串GetOrder()
        {
           返回repo.GetOrderList();
        }

       公共无效AddOrder(Order _orders)
        {
            repo.AddMagentoOrder(_orders);
        }
    }

public class OrderApi : IOrderApi
    {
        static IOrderRepository repo = new OrderRepository();

        public string GetOrder()
        {
            return repo.GetOrderList();
        }

        public void AddOrder(Order _orders)
        {
            repo.AddMagentoOrder(_orders);
        }
    }

--------------------------------------------------- ----------------

---------------------------------------------------------------

OrderRepository.cs

OrderRepository.cs

--------------------------------------------------- -----------

----------------------------------------------------------

PHP卷曲帖子

--------------------------------------------------- -------------

------------------------------------------------------------

<?php 

$ url ='http://crm.ultimocreations.com/api/orderapi.svc/addorders/';
$ orders_data = array(
评论" => 你好",
'Country'=> '印度',
'CustomerId'=> 2147,
'Days'=> 4,
'EmailId'=> 'suraj@g.com',
名称" => 'suraj singh',
'OrderDate'=> '16 -05-2017',
'OrderSatus'=> '成功',
'OrderTotal'=> 21321442,
'Order_Id'=> 'N768903',
'Order_ItemId'=> 123123,
'PaymentMethod'=> 'dd',
'PromiseDate'=> '20 -05-2017',
'SMSID'=> 43255
);
//根据您的API,客户数据应采用这种结构
$ data = array('Order'=> $ orders_data);
$ data_string = json_encode($ data);
$ getResponse = _jsoncurl($ url,$ data_string);
$ getDecodeContact = json_decode($ getResponse);
echo $ getDecodeContact;


函数_jsoncurl($ url_,$ data_)
{
$ ch = curl_init($ url_);
curl_setopt($ ch,CURLOPT_VERBOSE,1);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,FALSE); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ ch,CURLOPT_POST,TRUE);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data_);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array("Content-Type:application/json")));
$ response = curl_exec($ ch);
curl_close($ ch);
返回$ response;
}

?>

<?php 

$url = 'http://crm.ultimocreations.com/api/orderapi.svc/addorders/';
$orders_data = array(
'Comment' => 'hello',
'Country' => 'india',
'CustomerId' => 2147,
'Days' => 4,
'EmailId' => 'suraj@g.com',
'Name' => 'suraj singh',
'OrderDate' => '16-05-2017',
'OrderSatus' => 'success',
'OrderTotal' => 21321442,
'Order_Id' => 'N768903',
'Order_ItemId' => 123123,
'PaymentMethod' => 'dd',
'PromiseDate' => '20-05-2017',
'SMSID' => 43255
);
// As per your API, the customer data should be structured this way
$data = array('Order' => $orders_data);
$data_string = json_encode($data);
$getResponse = _jsoncurl($url, $data_string);
$getDecodeContact = json_decode($getResponse);
echo $getDecodeContact;


function _jsoncurl($url_, $data_)
{
$ch = curl_init($url_);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

?>

--------------------------------------------------- -------------

------------------------------------------------------------

我要发布具有订单类型数据类型的订单数据.请给我建议解决方案.

I want to post order data that have order type data type. Kindly suggest me solution.


推荐答案

老实说,我对PHP不熟悉,建议您尝试通过在Fiddler中发送请求来缩小此问题的范围.或PostMan检查Rest Service是否正常.

To be honesty, I am not familiar with PHP, I would suggest you try to narrow down this issue by sending request in Fiddler or PostMan to check whether the Rest Service is OK.

这是我的测试.

        [WebInvoke(Method = "POST", UriTemplate = "/PostJson",RequestFormat =WebMessageFormat.Json)]
        string PostJson(Headers headers);

        public string PostJson(Headers headers)
        {
            return headers.loginname;
        }

POST /WCFRest/RestService.svc/PostJson HTTP/1.1
Host: localhost
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: b253a609-b7f5-b472-520c-9b3a4b18c871

{
  "loginname": "test",
  "password": "123"
}

如果它可以在Fiddler或PostMan中使用,我建议您尝试使用Fiddler从php捕获请求以检查差异.

If it works in Fiddler or PostMan, I would suggest you try to use Fiddler to capture the request from your php to check the difference.


这篇关于获得空值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆