doutongya8378 2015-05-02 20:47
浏览 85
已采纳

更好地将CURL转换为WCF

I am able to post to a web-service I wrote in C# from PHP via a CURL request, but I cannot get the request to go through correctly. No matter what I do, the only way that I can get the post to show up is if I json_encode the form data headers. So my PHP code then ends up like so:

$user = array('id'=>5, 'first_name' => 'First', 'last_name' => 'Last');

$uch = curl_init();
curl_setopt($uch, CURLOPT_URL,"http://localhost:50115/Service1.svc/postUser");
curl_setopt($uch, CURLOPT_POST, 1);
curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode("user=" . json_encode($user)));
curl_setopt($uch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json'
                                            ));
curl_setopt($uch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($uch);

When I set the line:

curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode("user=" . json_encode($user)));

to:

curl_setopt($uch, CURLOPT_POSTFIELDS, array("user" => json_encode($user)));
--OR--
curl_setopt($uch, CURLOPT_POSTFIELDS, 
                    http_build_query(array("user" => json_encode($user))));
--OR--
curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode($user));

there is no error in the PHP, the C# service does not return an error (I do have the setting to return exceptions set in the config), and the break-point on the function never gets hit.

On the C# side, I have my interface and service set up like so:

//Interface
[OperationContract]
[WebInvoke(Method="POST",
            BodyStyle=WebMessageBodyStyle.Bare, //Any other value, the break-point 
                                                //will not trigger
            RequestFormat=WebMessageFormat.Json,
            UriTemplate = "postUser")]
void PostUser(string user);

//Service
public void PostUser(string user)
{
    var request = user;
    int i = 5; //Debug set here
}

When the break-point hits, I am able to see that user has the value of:

user={"id":5,"first_name":"First","last_name":"Last"}

What I would like to have is for the value of user to simply be:

{"id":5,"first_name":"First","last_name":"Last"}

Is it possible to receive my response like this? If so, what do I need to change to accomplish it?

  • 写回答

1条回答 默认 最新

  • doubeng3412 2015-05-02 22:30
    关注

    After trying multiple combinations in PHP and C#, I finally got it to work. In PHP, the CURLOPT_POSTFIELDS needed to be set with:

    curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode(array("user" => json_encode($user))));
    

    Then, in C#, I needed to change BodyStyle=WebMessageBodyStyle.Bare to BodyStyle=WebMessageBodyStyle.Wrapped;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题