php 获取httpwebrequest数据,使用HttpWebRequest将json发送到php symfony2-如何在php中获取json...

Dictionary data = JsonConvert.DeserializeObject>(json);

if (data["ReturnValue"].Equals("0"))

{

List m_ninushis = new M_NINUSHI_DAO().GetList(data["LastUpdateDate"]);

string data_m_ninushi = JsonConvert.SerializeObject(m_ninushis);

string sentResponse = Util.FilterData(data_m_ninushi);

Dictionary dataResponse = JsonConvert.DeserializeObject>(sentResponse);

if (dataResponse["ReturnValue"].Equals("0"))

{

return 0;

}

else

{

return 1;

}

}

this id my code in webservice use asp.net. I use HttpWebRequest send data to symfony2 api

FilterData

XElement xdoc = XElement.Load(configFileName);

var objStringConnection = xdoc.Descendants("URL").Select(e => new { filter_data = e.Descendants("URL_FILTER_DATA").FirstOrDefault().Value }).SingleOrDefault();

string urlAddress = objStringConnection.filter_data;

System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);

Dictionary json = new Dictionary();

json.Add("M_Ninushi", data);

byte[] dataSent = Encoding.ASCII.GetBytes(json.ToString());

request.Method = "POST";

request.ContentType = "application/x-www-form-urlencoded";

//application/x-www-form-urlencoded

request.ContentLength = dataSent.Length;

Stream writer = request.GetRequestStream();

writer.Write(dataSent, 0, dataSent.Length);

writer.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)

{

Stream receiveStream = response.GetResponseStream();

StreamReader readStream = null;

if (response.CharacterSet == null)

readStream = new StreamReader(receiveStream);

else

readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));

string dataResponse = readStream.ReadToEnd();

response.Close();

readStream.Close();

return dataResponse;

}

this id my code in webservice use asp.net. I use HttpWebRequest send data to symfony2 api

I know how to send data but I don't know how to get data in symfony2 . someone help me

Solutions1

C# Code Corrections

First, we need to correct the Content-Type sent to the server hosting the Symfony2 application. The data you are sending is not in the proper format for application/x-www-form-urlencoded. Change it to application/json.

Also, JSON data MUST be encoded in Unicode. In PHP, json_decode() only supports UTF-8 encoded strings. Therefore you must use Encoding.UTF8.GetBytes instead of Encoding.ASCII.GetBytes.

Dictionary.toString() does not return a JSON string. Use Json.NET.

Receiving JSON Data in Symfony2

In your Controller, you can use Symfony\Component\HttpFoundation\Request::getContent() to retrieve the form content.

namespace Company\CodeExampleBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpKernel\Exception\HttpException;

class RestAPIController extends Controller

{

public function doSomethingInterestingAction(Request $request)

{

if($request->headers->get('Content-Type') !== 'application/json') {

throw $this->createBadRequestException();

}

$jsonData = json_decode($request->getContent(), true);

if($jsonData === null) {

throw $this->createBadRequestException();

}

// DO SOMETHING WITH $jsonData

}

protected function createBadRequestException()

{

return new HttpException(400, 'This endpoint expects JSON data in the POST body and requires Content-Type to be set to application/json.');

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值